需求:
对于创建快捷方式到桌面,网上能查到不少资料,但一般都是针对应用程序本身的。
前阵子在做项目时,遇到了一个类似于百度贴吧里面的一个需求:对于每个具体的贴吧,都可以将其发送到桌面(HomeScreen)建立快捷方式shortcut。
图标相同,只是图标下面显示的名称为具体贴吧的名称,然后点击此快捷图标则能直接进入到本贴吧中。
实现:
1.AndroidManifest中声明权限:
1 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
2 <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
2.app_start.xml布局文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:gravity="center_horizontal"
6 android:orientation="vertical"
7 android:paddingBottom="20dp"
8 android:paddingTop="20dp" >
9
10 <TextView 11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:text="这是主activity" />
14
15 <Button 16 android:id="@+id/button1"
17 android:layout_width="200dp"
18 android:layout_height="wrap_content"
19 android:layout_marginTop="30dp"
20 android:text="进入第一个贴吧" >
21 </Button>
22
23 <Button 24 android:id="@+id/button2"
25 android:layout_width="200dp"
26 android:layout_height="wrap_content"
27 android:layout_marginTop="15dp"
28 android:text="进入第二个贴吧" >
29 </Button>
30
31 </LinearLayout>
相应的逻辑代码为:
1 package com.qqyumidi.shortcutdemo; 2
3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9
10 public class AppStart extends Activity implements OnClickListener { 11
12 private Button button1; 13 private Button button2; 14
15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.app_start); 19
20 button1 = (Button) findViewById(R.id.button1); 21 button2 = (Button) findViewById(R.id.button2); 22
23 button1.setOnClickListener(this); 24 button2.setOnClickListener(this); 25
26 Intent intent = getIntent(); 27 if (intent != null) { 28 String tName = intent.getStringExtra("tName"); 29 if (tName != null) { 30 Intent redirectIntent = new Intent(); 31 redirectIntent.setClass(AppStart.this, TiebaActivity.class); 32 redirectIntent.putExtra("tName", tName); 33 startActivity(redirectIntent); 34 } 35 } 36 } 37
38 @Override 39 public void onClick(View v) { 40 // TODO Auto-generated method stub
41 Intent intent = new Intent(); 42 intent.setClass(AppStart.this, TiebaActivity.class); 43 //传参
44 switch (v.getId()) { 45 case R.id.button1: 46 intent.putExtra("tName", "玉米吧"); 47 break; 48 case R.id.button2: 49 intent.putExtra("tName", "土豆吧"); 50 break; 51 } 52 startActivity(intent); 53 } 54 }
贴吧页tieba_activity.xml布局文件为:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:gravity="center_horizontal"
6 android:orientation="vertical"
7 android:paddingBottom="20dp"
8 android:paddingTop="20dp" >
9
10 <TextView 11 android:id="@+id/textview1"
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content" />
14
15 <Button 16 android:id="@+id/button3"
17 android:layout_width="200dp"
18 android:layout_height="wrap_content"
19 android:layout_marginTop="30dp"
20 android:text="生成快捷方式到桌面" >
21 </Button>
22
23 </LinearLayout>
相应的逻辑代码为:
package com.qqyumidi.shortcutdemo; import android.app.Activity; import android.content.Intent; import android.content.Intent.ShortcutIconResource; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TiebaActivity extends Activity { private TextView textView; private Button button; private String tName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tieba_activity); Intent intent = getIntent(); tName = intent.getStringExtra("tName"); textView = (TextView) findViewById(R.id.textview1); textView.setText("本贴吧名称: " + tName); button = (Button) findViewById(R.id.button3); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub
if (tName == null) { tName = getString(R.string.app_name); } addShortCut(tName); } }); } private void addShortCut(String tName) { // 安装的Intent
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // 快捷名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, tName); // 快捷图标是允许重复
shortcut.putExtra("duplicate", false); Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); shortcutIntent.putExtra("tName", tName); shortcutIntent.setClassName("com.qqyumidi.shortcutdemo", "com.qqyumidi.shortcutdemo.AppStart"); shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); // 快捷图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); // 发送广播
sendBroadcast(shortcut); } }
在如上代码中,最主要的部分为addShortCut()函数和AppStart对传入参数的相应处理过程。
ShortCut Demo下载地址:点击下载