需求:

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

前阵子在做项目时,遇到了一个类似于百度贴吧里面的一个需求:对于每个具体的贴吧,都可以将其发送到桌面(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 发送多个不同的快捷方式(shortcut)到桌面并向其启动的Activity传参的更多相关文章

  1. Android添加快捷方式(Shortcut)到手机桌面

    Android添加快捷方式(Short)到手机桌面 权限 要在手机桌面上添加快捷方式,首先需要在manifest中添加权限. <!-- 添加快捷方式 --> <uses-permis ...

  2. Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity

    我们一般下载的应用在第一次启动应用的时候都会给我创建一个桌面快捷方式,然后我在网上找了些资料整理下了,写了一个快捷方式的工具类,这样我们以后要创建快捷方式的时候直接拷贝这个类,里面提供了一些静态方法, ...

  3. Android 主界面长按创建快捷方式

    Android中创建快捷方式主要有两种方式.一是在代码中直接加入生成桌面快捷方式的代码:二是通过小部件加入; 这篇文章主要讲另外一种方法! 1.通过在AndroidManifest文件里为Activi ...

  4. android发送/接收json数据

    客户端向服务器端发送数据,这里用到了两种,一种是在url中带参数,一种是json数据发送方式: url带参数的写法: url+/?r=m/calendar/contact_list&uid=3 ...

  5. android发送/接收Json包含中文的处理

    转自:http://wiki.neal365.com/2013/02/25/android%E5%8F%91%E9%80%81%E6%8E%A5%E6%94%B6json%E5%8C%85%E5%90 ...

  6. android 发送短信 怎样做到一条一条的发送,仅仅有在上一条发送成功之后才发送下一条短信

    android发送短信截获上一条发送是否成功,然后再来发送下一条短信 1.问题:在项目中遇到例如以下要求:待发短信有N条,实现一条一条的发送并在上一条短信发送成功之后再来发送下一条. for(int ...

  7. Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)

    Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件第二版 上次粗略的写了相同功能的代码,这次整理修复了之前的一些BUG,结构也大量修改 ...

  8. android studio 添加get,set方法快捷方式

    android studio 添加get,set方法快捷方式

  9. android发送与接收超长短信

    android发送与接收超长短信 android接收发送短信,支持的最大字符数是70个,实际是67个字符,如果发送的短信超过了该数目,那就需要用到sendMultipartTextMessage()方 ...

随机推荐

  1. Linux虚拟机搭建本地yum源

    Yum本地源的配置 本教程是在虚拟机里安装Red Hat Enterprise Linux 7 ,以其为例使用iso文件进行Yum本地源的配置.所使用的软件如下: (1)虚拟机:Vmware work ...

  2. HDU-1423 最长公共上升子序列(LCIS)

    问题描述: 给定两个字符串x, y, 求它们公共子序列s, 满足si < sj ( 0 <= i < j < |s|).要求S的长度是所有条件序列中长度最长的. 做过最长公共子 ...

  3. sqlzoo:3

    顯示1980年物理學(physics)獲獎者,及1984年化學獎(chemistry)獲得者. select yr,subject,winner from nobel ) ) 查看1980年獲獎者,但 ...

  4. C# 中传参中的OUT 和 ref 区别 笔记

    //out传参前需要对参数进行赋值处理,ref则不需要.//out.ref 传参都可以对值进行改变 1 static void Main(string[] args) { ; //int J = 10 ...

  5. PHP中使用CURL之php curl详细解析

    在正式讲怎么用之前啊,先提一句,你得先在你的PHP环境中安装和启用curl模块,具体方式我就不讲了,不同系统不同安装方式,可以google查一下,或者查阅PHP官方的文档,还挺简单的. 1. 拿来先试 ...

  6. json格式的数据及遍历:

    代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  7. [bzoj1088]扫雷

    额,这种水题我也不说什么了233 Description 相信大家都玩过扫雷的游戏.那是在一个n*m的矩阵里面有一些雷,要你根据一些信息找出雷来.万圣节到了,“余”人国流行起了一种简单的扫雷游戏,这个 ...

  8. Qt5+MSVC2015环境将VS2015编写的控制台程序转化为GUI程序

    如题所述,如何将VS2015编写的控制台程序转化为Qt5+MSVC2015环境编译的GUI程序? 最近想到这个操作,类似于Linux下使用的命令行操作转到Windows下使用GUI操作,看了控制台的命 ...

  9. Web版需求征集系统所得2,servlet中request.getParameter获值乱码问题解决

    servlet获值乱码问题解决 解决办法一(最简单有效) request.setCharacterEncoding("utf-8"); 解决办法二 因为乱码问题的产生是因为默认格式 ...

  10. 如何实现文件上传 - JavaWeb

    直接上代码 ( idea 开发,SpringBoot 框架 ): 首先是Controller的写法: package com.xxx.Controller; import com.xxx.Tools. ...