Recently, I found it interesting to add shortcuts. I checked the information and wrote a demo-a simple example. There are two buttons in this example, click "add this program to shortcut". A shortcut is added to the desktop of the mobile phone, and a shortcut is added to the launcher. Click "Exit" and the prompt is displayed: the toast prompts "Exit program ". Knowledge sorting: There are two ways to add shortcuts to the Android platform: one is Desktop shortcuts and the other is launcher shortcuts. Principle: Some information is encapsulated by intent, and the launcher is notified to create a shortcut in the form of broadcast! Do not forget to register the permission in manifest. xml:
<Uses-Permission Android: Name = "com. Android. launcher. Permission. install_shortcut">
Add an action-filtered intentfilter to manifest. xml. The shortcut list contains multiple shortcuts for this program.
If you have any questions or want to say something, you can leave a message. You are welcome to criticize and correct it. repost it and indicate the source:
Let's take a look at the program:
On the program start page, click "add shortcuts to the program:
Click the exit button, and there is a shortcut on the desktop. Play toast: click out to select the shortcut, and the program shortcut is added:
In the intentwidget project:
1. The code in intentwidgetmainactivity. Java in the com.cn. Daming package:
package com.cn.daming;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;import android.widget.Toast;public class IntentWidgetMainActivity extends Activity implements OnClickListener{private Button mStartWidgetButton;private Button mExitButton;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mStartWidgetButton = (Button) findViewById(R.id.my_button_1); mExitButton = (Button) findViewById(R.id.my_button_2); mStartWidgetButton.setOnClickListener(this); mExitButton.setOnClickListener(this); }public void onClick(View v){if(v == mStartWidgetButton){//inint the widgetIntent is declearIntent addWidgetIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //whether repeat create is or not addWdgetIntent.putExtra("duplicate",true);//set the Widget of the titleString mTitle = getResources().getString(R.string.my_title);//set the Widget of the iconParcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.widget_image);Intent mIntent = new Intent(this,IntentWidgetMainActivity.class);addWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mTitle);//set the titleaddWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//set the iconaddWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mIntent);//set the intentsendBroadcast(addWidgetIntent);}else if(v == mExitButton){finish();Toast.makeText(IntentWidgetMainActivity.this, R.string.exit, Toast.LENGTH_SHORT).show();}}}
Ii. Code in Main. xml under the layout directory:
<?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" android:background="#00ffffff" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="15dip" android:layout_marginBottom="15dip" android:gravity="center" android:text="@string/hello" android:textSize="8pt" /><Button android:id="@+id/my_button_1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:textSize="10pt" android:text="@string/my_button_1"/><Button android:id="@+id/my_button_2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:textSize="10pt" android:text="@string/my_button_2"/><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dip" android:gravity="center" android:text="@string/blogs" android:textSize="8pt" /></LinearLayout>
Iii. Code in string. xml under values:
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> This is the shortcut that Daming adds to launcher </string> <string name = "app_name"> Daming shortcut! </String> <string name = "my_button_1"> Add a shortcut to the Program </string> <string name = "my_button_2"> exit the Program </string> <string name = "my_title"> Daming Program </string> <string name = "exit"> the program is exiting ...... </String> <string name = "blogs"> blog address: \ n http://blog.csdn.net/wdaming1986/article/details/6877154 </string> </resources>
4. Code in manifest. xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cn.daming" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".IntentWidgetMainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- add the launch of my programmer`s quick launcher--> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT"/> </intent-filter> </activity> </application> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/></manifest>