Android 发送多个不同的快捷方式(shortcut)到桌面并向其启动的Activity传参

简介:

需求:

对于创建快捷方式到桌面,网上能查到不少资料,但一般都是针对应用程序本身的。

前阵子在做项目时,遇到了一个类似于百度贴吧里面的一个需求:对于每个具体的贴吧,都可以将其发送到桌面(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下载地址:点击下载

 

---------------------------------------------------------------------------------
笔者水平有限,若有错漏,欢迎指正,如果转载以及CV操作,请务必注明出处,谢谢!

本文转自Windstep博客园博客,原文链接:http://www.cnblogs.com/lwbqqyumidi/p/3358310.html,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
42 1
|
3月前
|
数据库 Android开发 开发者
Android基础知识:请解释Activity的生命周期。
Android基础知识:请解释Activity的生命周期。
43 2
|
4月前
|
XML Java Android开发
Android Studio App开发之捕获屏幕的变更事件实战(包括竖屏与横屏切换,回到桌面与切换到任务列表)
Android Studio App开发之捕获屏幕的变更事件实战(包括竖屏与横屏切换,回到桌面与切换到任务列表)
39 0
|
6月前
|
存储 SQL 人工智能
Android Activity启动流程一:从Intent到Activity创建
Android Activity启动流程一:从Intent到Activity创建
|
2月前
|
Android开发
[Android 四大组件] --- Activity
[Android 四大组件] --- Activity
22 1
|
3月前
|
Android开发
Android基础知识:什么是Fragment?与Activity的区别是什么?
Android基础知识:什么是Fragment?与Activity的区别是什么?
285 54
|
4月前
|
XML 安全 Java
Android Studio App开发入门之活动Activity中为活动补充附加信息讲解及实战(附源码 超详细必看)
Android Studio App开发入门之活动Activity中为活动补充附加信息讲解及实战(附源码 超详细必看)
35 0
|
4月前
|
Android开发
Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
44 0
|
4月前
|
Android开发
Android Studio APP开发入门之活动Activity中启停活动页面的讲解及实战(附源码,包括Activity的启动结束、生命周期、跳转等)
Android Studio APP开发入门之活动Activity中启停活动页面的讲解及实战(附源码,包括Activity的启动结束、生命周期、跳转等)
41 0
|
5月前
|
Android开发
安卓activity管理器
安卓activity管理器
27 0