需求:

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

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

    Description: 给你2个长度为n的01串 从中选出\(n/2\)个,使得选出的数中第一排1的个数等于未选出数中第二排1的个数 输出一种方案即可,没有输出-1 Hint: \(n \le 50 ...

  2. 快速安装puppeteer (跳过安装Chromium)

    npm i --save puppeteer --ignore-scripts

  3. SSM框架mapper.xml模糊查询语句

    SSM框架mapper.xml模糊查询语句 在用SSM框架时,如果想要实现模糊查询,可以在mapper.xml文件中进行数据库语句的书写,方法有很多种,在这里我选择了两种介绍: 方法1: <se ...

  4. oc中的反射机制

    好久没有总结过了,一直在赶项目... 今天来总结一下OC中的反射机制,有什么不对的地方,还请多多海涵. 反射机制,简单的说就是在程序运行期间通过类的名字来动态的获取类的信息,从而实现动态的创建类,以及 ...

  5. ionic基于GPS定位并通过百度地图获取定位详细信息

    相信所有的前端攻城狮都会碰到移动端App.里面获取用户定位信息. 那么问题来了,怎么获取用户的定位信息(经纬度)呢. 当然方法有很多,通过百度地图API 以及 高德地图 API都是可以的.但是两个获取 ...

  6. php GD库快速消耗CPU资源漏洞 CVE-2018-5711测试

    漏洞说明: 用一张GIF图片就可导致服务器发生崩溃直至宕机,在现实中非常容易利用. 影响版本: PHP 5 < 5.6.33 PHP 7.0 < 7.0.27 PHP 7.1 < 7 ...

  7. vim 常用指令

    其他命令 <c-L> 重绘屏幕 <c-z> 挂起vim回到shell,想继续vim只需要输入 fg <c-x-f> 文件路径提示 <c-N> 当前文件中 ...

  8. [Codeforces Round #516][Codeforces 1063C/1064E. Dwarves, Hats and Extrasensory Abilities]

    题目链接:1063C - Dwarves, Hats and Extrasensory Abilities/1064E - Dwarves, Hats and Extrasensory Abiliti ...

  9. ubuntu amd64 的锐捷连接解决办法---武汉大学

    昨日博主闲来弄了个ubuntu玩玩,于是上网成了个问题,博主武大信息学部,锐捷上校园网.装的是13.04的amd64. 凑巧在珞珈山水bbs上看到我在解决上网出现问题出现的相同情况,但是没有人回答,于 ...

  10. PHP 点阵5*7字体

    效果: 源码: <?php // standard ascii 5x7 font 纵向取模 // defines ascii characters 0x20-0x7f (32-127) $fon ...