Android ways to create shortcuts through applications _android

Source: Internet
Author: User
Tags xmlns

The examples in this article describe how Android creates shortcuts through applications. Share to everyone for your reference. Specifically as follows:

Android shortcuts are the most basic components of the desktop. It is used to directly start a component of an application.

In general, you can create a shortcut to the application on the left by long pressing an icon for one application on the list of launcher applications. In addition, you can add shortcuts to your desktop in two ways:

One: Create a intent in the application, and then notify Launcher to create a shortcut in broadcast form.

Two: Register a intentfilter for the application's components, and then add a shortcut to start the component directly on the Launcher desktop.

The following simulation adds a shortcut to the application

Main.xml Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:orientation=" vertical " 
 android:layout_width=" fill_parent "
 android:layout_" height= "Fill_parent" >
 <button android:id= "@+id/createshortcut" android:layout_width= "Fill_parent"
  android:layout_height= "wrap_content"
  android:layout_gravity= "Center_horizontal"
  android:textsize= "20px"
  android:text= "create shortcut Keys"/>
 <button android:id= @+id/exit "android:layout_width=" Fill_
  Parent "
  android:layout_height=" wrap_content "
  android:layout_gravity=" Center_horizontal "
  Android : textsize= "20px"
  android:text= "Exit"/>
</LinearLayout>

Manifest file:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 package=" Com.ljq.action "android:versioncode=" 1 "
 android:versionname=" 1.0 ">
 < Application android:icon= "@drawable/icon"
  android:label= "@string/app_name" >
  <activity android:name =". Shortcutaction "
   android:label=" @string/app_name ">
   <intent-filter>
    <action android:name= "Android.intent.action.MAIN"/>
    <category
     android:name= "Android.intent.category.LAUNCHER"/>
   </intent-filter>
  </activity>
 </application>
 <uses-sdk android: minsdkversion= "7"/>
 <!--add a shortcut key permission-->
 <uses-permission
  android:name= " Com.android.launcher.permission.INSTALL_SHORTCUT "/>
</manifest>

Shortcutaction class:

Package com.ljq.action;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import android.os.Parcelable;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button; /** * Create shortcuts via application * * @author Jiqinlin */public class Shortcutaction extends activity implements Onclicklistener {Private button Createshortcut=null//Create Shortcut button Private button exit=null;//exit system @Override public void OnCreate (Bundle
  Savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
  createshortcut= (Button) Findviewbyid (r.id.createshortcut);
  exit= (Button) Findviewbyid (r.id.exit);
  Createshortcut.setonclicklistener (this);
 Exit.setonclicklistener (this);
  public void OnClick (View v) {//button btn= (Button) v;
   Switch (V.getid ()) {case r.id.createshortcut://string title=getresources (). getString (R.string.title); Intent addintent=new Intent ("Com.android.launcher.action.INSTALL_SHORTCUT"); Parcelable Icon=intent.shortcuticonresource.fromcontext (this, r.drawable.png);
   Gets the icon for the shortcut key Intent myintent=new Intent (this, shortcutaction.class); Addintent.putextra (Intent.extra_shortcut_name, "shortcuts");//Shortcut title Addintent.putextra (intent.extra_shortcut_icon_ RESOURCE, icon);//Shortcut Icons Addintent.putextra (intent.extra_shortcut_intent, myintent);//Shortcut Action Sendbroadcast (
  addintent);/send broadcast break;
   Case R.id.exit:system.exit (0);
  Break

 }
 }
}

Run Result:

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.