最近感觉这个添加快捷方式挺有趣的,就查资料自己写了个demo---简单的例子,这个例子就是有两个按钮,点击“将此程序添加到快捷方式”,则手机桌面增加一个快捷方式,同时launcher中也多了一个快捷方式,点击退出,则提示:toast弹提示信息“退出程序”。知识梳理:Android平台上添加快捷方式有两种:一种桌面的快捷方式,一种是launcher的快捷方式。原理:是通过intent封装一些信息,以Broadcast的形式通知launcher创建快捷方式的!一定不要忘记在manifest.xml中注册一下权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT">
在manifest.xml中加入一个动作过滤的intentFilter,快捷方式的列表中会多个该程序的快捷方式。
有问题或向说点什么的可以留言,欢迎大家批评和指正,转载请标明出处:
下面看一下程序的截图:
程序的开始界面: 点击“将此程序添加快捷方式”按钮:
点击退出按钮,桌面多了快捷方式,弹Toast: 点出选择快捷方式后多了程序的快捷方式:
在IntentWidget工程中:
一、在com.cn.daming包中IntentWidgetMainActivity.java中的代码:
- <span><span>
- </span></span><span style="font-size:13px;color:#000000;">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 declear
- Intent addWidgetIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
- <span style="white-space: pre-wrap;"></span>//whether repeat create is or not
- addWdgetIntent.putExtra("duplicate",true);<span style="font-family: monospace;font-size:10px;"><span style="white-space: pre-wrap;"><span><span>
- </span></span></span></span>
- //set the Widget of the title
- String mTitle = getResources().getString(R.string.my_title);
- //set the Widget of the icon
- Parcelable 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 title
- addWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//set the icon
- addWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mIntent);//set the intent
- sendBroadcast(addWidgetIntent);
- }
- else if(v == mExitButton){
- finish();
- Toast.makeText(IntentWidgetMainActivity.this, R.string.exit, Toast.LENGTH_SHORT).show();
- }
- }
- }</span>
二、在layout目录下的main.xml中的代码:
- <?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>
三、在values下的string.xml中的代码:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">这是大明添加到Launcher的快捷方式</string>
- <string name="app_name">大明快捷方式!</string>
- <string name="my_button_1">将此程序添加快捷方式</string>
- <string name="my_button_2">退出程序</string>
- <string name="my_title">大明程序</string>
- <string name="exit">程序正在退出。。。。。。</string>
- <string name="blogs">博客地址:\n http://blog.csdn.net/wdaming1986/article/details/6877154</string>
- </resources>
四、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>