Android 发送多个不同的快捷方式(shortcut)到桌面并向其启动的Activity传参
2013-10-11 20:05
459 查看
需求:
对于创建快捷方式到桌面,网上能查到不少资料,但一般都是针对应用程序本身的。
前阵子在做项目时,遇到了一个类似于百度贴吧里面的一个需求:对于每个具体的贴吧,都可以将其发送到桌面(HomeScreen)建立快捷方式shortcut。
图标相同,只是图标下面显示的名称为具体贴吧的名称,然后点击此快捷图标则能直接进入到本贴吧中。
实现:
1.AndroidManifest中声明权限:
2.app_start.xml布局文件:
相应的逻辑代码为:
贴吧页tieba_activity.xml布局文件为:
相应的逻辑代码为:
在如上代码中,最主要的部分为addShortCut()函数和AppStart对传入参数的相应处理过程。
ShortCut Demo下载地址:点击下载
对于创建快捷方式到桌面,网上能查到不少资料,但一般都是针对应用程序本身的。
前阵子在做项目时,遇到了一个类似于百度贴吧里面的一个需求:对于每个具体的贴吧,都可以将其发送到桌面(HomeScreen)建立快捷方式shortcut。
图标相同,只是图标下面显示的名称为具体贴吧的名称,然后点击此快捷图标则能直接进入到本贴吧中。
实现:
1.AndroidManifest中声明权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
2.app_start.xml布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:paddingBottom="20dp" android:paddingTop="20dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是主activity" /> <Button android:id="@+id/button1" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="进入第一个贴吧" > </Button> <Button android:id="@+id/button2" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="进入第二个贴吧" > </Button> </LinearLayout>
相应的逻辑代码为:
package com.qqyumidi.shortcutdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class AppStart extends Activity implements OnClickListener { private Button button1; private Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.app_start); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); Intent intent = getIntent(); if (intent != null) { String tName = intent.getStringExtra("tName"); if (tName != null) { Intent redirectIntent = new Intent(); redirectIntent.setClass(AppStart.this, TiebaActivity.class); redirectIntent.putExtra("tName", tName); startActivity(redirectIntent); } } } @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(AppStart.this, TiebaActivity.class); //传参 switch (v.getId()) { case R.id.button1: intent.putExtra("tName", "玉米吧"); break; case R.id.button2: intent.putExtra("tName", "土豆吧"); break; } startActivity(intent); } }
贴吧页tieba_activity.xml布局文件为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:paddingBottom="20dp" android:paddingTop="20dp" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button3" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="生成快捷方式到桌面" > </Button> </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下载地址:点击下载
相关文章推荐
- Android创建桌面快捷方式并像启动Activity传递参数
- android 中Activity 五种不同的启动方式
- Android应用启动后自动创建桌面快捷方式
- android桌面快捷方式跳转到指定activity
- Android添加快捷方式(Shortcut)到手机桌面
- Android APP自动生成桌面快捷图标 启动方式有差异
- Android应用启动后自动创建桌面快捷方式
- Android应用启动后自动创建桌面快捷方式
- [Android] Android应用启动后自动创建桌面快捷方式
- android创建桌面快捷方式(启动目标非项目的启动页)
- Android 为应用程序中的任意Activity创建桌面快捷方式
- android -- 小功能 为Activity生成桌面快捷方式
- Android应用启动后自动创建桌面快捷方式
- Android开发:应用启动后自动创建桌面快捷方式(三星机常见)
- android 软件 安装 卸载 启动 分享 桌面快捷方式
- 解析Android应用启动后自动创建桌面快捷方式的实现方法
- Android应用启动后自动创建桌面快捷方式
- Android不同方式启动导致重复启动相同的Activity的问题解决办法(转载,解决了我的问题)
- android 桌面快捷方式、窗口小部件、文件夹、书签 launcher、appwidget、shortcut、folder、bookmark