需求:

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

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

    小马哥STM32课程系列 http://www.moore8.com/courses/1308

  2. Bad Hair Day [POJ3250] [单调栈 或 二分+RMQ]

    题意Farmer John的奶牛在风中凌乱了它们的发型……每只奶牛都有一个身高hi(1 ≤ hi ≤ 1,000,000,000),现在在这里有一排全部面向右方的奶牛,一共有N只(1 ≤ N ≤ 80 ...

  3. php获取当前时间的毫秒数

    floor(microtime()*1000); 用microtime能输出当前的秒的后面8位小数 乘以1000取整数就行了

  4. 图解Raft之领导者选举

    图解Raft领导者选举,这里通过五张图来解答Raft选举的全过程: Raft集群各个节点之间是通过RPC通讯传递消息的,每个节点都包含一个RPC服务端与客户端,初始时启动RPC服务端.状态设置为Fol ...

  5. DAO模式

    什么是DAO模式: DAO(Data Access Object Pattern)用于将低层的数据操作API与上层的业务逻辑层分离,其主要涉及以下几个部分: 1.Data Access Object ...

  6. Java语法细节 - synchronized和volatile

    目录 synchronized关键字 关键字volatile synchronized关键字 synchronized关键字锁住方法和this的不同之处: public synchronized vo ...

  7. Mybatis中的Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 找不到Mapper.xml文件的问题

    1 首先在配置mapper-locations的时候: classpath:  只在现有目录下寻找配置文件 classpath*: 在现有的项目目录下和依赖的jar包下寻找xml配置文件

  8. Ubuntu16.04安装之后连不上无线网?有可能是Realtek rtl8822be的原因

    原以为昨天已基本写完在接触到Ubuntu以来遇到的所有问题了... 没想到今天去看有关ROS的资料时,居然无意间又看到了之前遇到的一个巨坑:安装完Ubuntu16.04之后,无线网用不了,根本无法连接 ...

  9. 36ArcGIS API for JavaScript3.X 系列加载天地图(经纬度)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. Unity进阶----DoTween及工程文件夹的建立(2018/11/12)

    DoTween 仅介绍部分常用用法,代码参上:(其它操作见官网:http://dotween.demigiant.com/documentation.php) using System.Collect ...