Android 为你的应用程序添加快捷方式【优先级高的快捷方式】

有人会说,快捷方式,不是安装完应用程序后,长按应用程序的ICON然后将它拖到桌面上不就行了吗?没错,这样是一种方法,但这种方法有一个缺点,看图吧:

如上图,如果我们长按桌面点击快捷方式,将会跳到如下界面,如果单从这个界面选择的话,我们就必须进入Applications 目录,然后再在Applications 里面选择我们对应的应用程序,这样的话用户可能得麻烦的去找咯。但我们同时会发现,在Applications 的下面有很多另外的ICON比如 上图的BookMark ,Contact 等,这些也是应用,那么这些是怎么做到不用进去Applications 而在第一页就出现供用户选择呢?今天我们就针对这点来讲讲吧。

 

要做这一功能首先我们先来了解一下manifest 里面的这一标签:

<activity-alias>

 

syntax:语法:
<activity-alias android:=["true" | "false"]
android:=["true" | "false"]
android:
="drawable resource"
android:="string resource"
android:
="string"
android:="string"
android:
="string" >
. . .
</activity-alias>
contained in:隶属于: <application> can contain:可以包含: <intent-filter>
<meta-data> description:说明: An alias for an activity, named by the targetActivity attribute. The target must be in the same application as the alias and it must be declared before the alias in the manifest.
activity的一个别名,用targetActivity属性命名。目标activity必须与别名在同一应用程序的manifest里,并且在别名之前声明。

The alias presents the target activity as a independent entity. It can have its own set of intent filters, and they, rather than the intent filters on the target activity itself, determine which intents can activate the target through the alias and how the system treats the alias. For example, the intent filters on the alias may specify the "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" flags, causing it to be represented in the application launcher, even though none of the filters on the target activity itself set these flags.
别名作为一个独立的实体代表目标activity。它可以有它自己的一套intent filter,它们,而不是目标activity自己的intent filter,决定哪个intent能够活性化目标通过别名以及系统如何处理别名。例如,别名的intent filter可以指定"android.intent.action.MAIN"和"android.intent.category.LAUNCHER"标签,使之显示在应用程序启动器上,即使目标activity自己没有设置这些标签。

With the exception of targetActivity, <activity-alias> attributes are a subset of <activity> attributes. For attributes in the subset, none of the values set for the target carry over to the alias. However, for attributes not in the subset, the values set for the target activity also apply to the alias.
targetActivity的例外,<activity-alias>属性是<activity>属性的一个子集。对于该子集中的属性,目标activity中设置的值不会覆盖别名的值。然而,对于那些子集中没有设置的属性,设置给目标activity的值同样适用于别名。

 

上面给出的解释我们来配置一下manifest,配置为如下:

 

<activity android:name=".shortcut">
            
<intent-filter>
                
<action android:name="android.intent.action.MAIN" />
            
</intent-filter>
        
</activity>

        
<activity-alias android:name=".CreateShortcuts"
            android:targetActivity
=".shortcut" android:label="@string/shortcut">

            
<intent-filter>
                
<action android:name="android.intent.action.CREATE_SHORTCUT" />
                
<category android:name="android.intent.category.DEFAULT" />
            
</intent-filter>

        
</activity-alias>

 

 

Activity:

.shortcut 是我们快捷方式需要的Activity

activity-alias:

 对应的targetActivity是指向应用创建快捷方式使用的Activity

android:label对应的创建快捷方式列表显示的文字,而该应用对应的快捷方式的图标则默认使用我们给定的application的图标。如图:

好了,这是第一步步骤,下面进入代码阶段,先看代码:

 

package com.terry.attrs;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.LinearLayout;
import android.widget.TextView;

public class shortcut extends Activity {
    
private static final String SHORT_CUT_EXTRAS = "com.terry.extra.short";

    @Override
    
protected void onCreate(Bundle savedInstanceState) {
        
// TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        final Intent intent 
= getIntent();
        final String action 
= intent.getAction();
        
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
            createShortCut();
            finish();
            
return;
        }

        String extra 
= intent.getStringExtra(SHORT_CUT_EXTRAS);
        LinearLayout layout 
= new LinearLayout(getApplicationContext());
        TextView tv 
= new TextView(getApplicationContext());

        
if (extra != null)
            tv.setText(extra);
        layout.addView(tv);
        setContentView(layout);
    }

    
void createShortCut() {
        Intent shortcutIntent 
= new Intent(Intent.ACTION_MAIN);
        shortcutIntent.setClass(
thisthis.getClass());
        shortcutIntent.putExtra(SHORT_CUT_EXTRAS, 
"测试的快捷方式");

        Intent intent 
= new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, 
"这里随便指定");
        Parcelable shortIcon 
= Intent.ShortcutIconResource.fromContext(
                
this, com.terry.attrs.R.drawable.icon);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortIcon);
        setResult(RESULT_OK, intent);
    }
}

 

 

代码解释:

onCreate方法,首先获取intent 的action如果接收到的action为创建快捷方式的请求,则执行创建快捷方式的代码,否则则通过得到的extra 为textView 赋值。

createShortCut方法,首先设置快捷方式点击后要跳转的intent 和要带入的参数,然后设置桌面快捷方式的名称,图标和对应的intent(即上面带入数据和跳转的界面的 class的Intent)最后将结果传入。

最近运行的结果:

跳击后到达的界面:

 

TIP:这里可以是任何ACTIVITY界面。

最后给大家分享下源码吧:

快捷方式

就到这里,希望我的一篇废话能对你有所帮助。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/465272.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

icinga2 php模块,在Ubuntu 18.04系统上安装Icinga2监视工具的方法

本文介绍在Ubuntu 18.04系统上安装Icinga2监视工具的方法&#xff0c;使用Icinga 2可以监控&#xff1a;服务器资源、网络服务、网络设备。简介Icinga 2是一个开源&#xff0c;可扩展和可扩展的监视工具&#xff0c;可检查网络资源的可用性&#xff0c;通知用户中断&#xff0c…

面试官问:malloc(0)时程序会返回什么?

今天跟大家找了篇文章&#xff0c;主要是一个面试中的有趣问题&#xff0c;其实有些问题在开发中没有遇到过会很难回答出来&#xff0c;如果在面试过程中回答正确&#xff0c;皆大欢喜&#xff0c;拿到offer的概率更大&#xff1b;回答不出来也不要信口开河&#xff0c;面试官主…

2018 Machine Learning

2018/8/13 线性模型(西瓜书P53~P73)Optimizerhttps://blog.csdn.net/u012151283/article/details/781549172018/8/15 SVM(西瓜书)2018/8/16 面试题 https://www.cnblogs.com/zuochongyan/p/5407053.html熵、联合熵、条件熵、交叉熵与相对熵 &#xff1f;&#xff1f;归一化方法…

考研失败了,怎么办?

有读者提到这个问题&#xff0c;顺带回答下。我没有考研过&#xff0c;但是身边有很多研究生和博士&#xff0c;额&#xff0c;还有很多海外留学的博士。前天我们有外部厂商来公司讨论合作&#xff0c;领导让我跟着一起介绍项目&#xff0c;对方的人问了一句&#xff1a;“你们…

10月28号日志

匆匆忙忙而来&#xff0c;怀着梦想、怀着希望来到苏州这个地方。想用自己在校学的知识来改变自己的命运&#xff0c;我空空而来想满载而归。在这一段的时间里&#xff0c;我深深感受到了同学情深&#xff0c;深似海。老师恩重重如山。在长生果科技有限公司工作已将近两个月了的…

在php中怎么用js跳转页面跳转,在php中怎么用redirect实现页面跳转?

1、thinkPHP 的Action类的redirect方法可以实现页面的重定向功能&#xff0c;redirect 重定向的通用语法为&#xff1a;edirect(url,paramsarray(),delay0,msg) // 跳转到 edit 操作 $this->redirect(edit)。2、// 跳转到 UserAction下的edit 操作 this->redirect(User/…

[BZOJ 4025] 二分图

题目传送-BZOJ4025 题意&#xff1a; 有一张\(n\)个节点的无向图,其中边\(i\)在\(s_i\)出现,\(e_i\)结束,并连接着节点\(x,y\). 并保证\(s_i < e_i \le T\),要求对于每个时间\(t\le T\)输出此时的图是否是二分图。\(n\le100000,m\le200000,T\le100000\) 题解&#xff1a; 这…

晒一波工程师的工位,你喜欢哪种?

程序员的圈子啊那是十分神秘&#xff0c;又令人着迷的。每天的工作就是对着电脑&#xff0c;那他们的工作是如何的呢&#xff1f;我们来品一品&#xff08;PS&#xff1a;后面奉上各位大佬的桌面&#xff0c;别走开哦&#xff09;↓↓↓最最常见的普通版&#xff1a;升级版&…

传360以原彩虹QQ研发团队为班底拟强推IM

据知情人士透露&#xff0c;奇虎360开发即时通讯工具IM软件已成定局&#xff0c;正式推出只是时间问题。同时&#xff0c;该知情人还透露&#xff0c;目前负责360公司即时通讯软件项 目的核心班底正是51.com原“彩虹QQ”&#xff08;51.com对外官方产品名称为“彩虹显IP辅助软件…

linux无法安装php-fpm,Linux下的php-fpm相关问题解决

今天搭建LNMP环境时,在安装PHP编译的时候出现了问题,首先在解压安装包后配置检测环境./configure --prefix/usr/local/php \--with-gd \--enable-gd-native-ttf \--enable-mysqlnd \--with-mysqlmysqlnd \--with-pdo-mysqlmysqlnd \--with-openssl \--enable-mbstring \--enabl…

recovery.conf 用于 stream replication

recovery.conf 是 postgresql slave 数据库的重要文件&#xff0c;示例文件为. $ ls -l $PGHOME/share/recovery.conf.sample可以编辑 $PGDATA/recovery.conf 异步stream recovery_target_timeline latest standby_mode on primary_conninfo host192.168.56.201 port5432 us…

彻底搞懂系统调用

在应用程序开发过程中经常会进行IO设备的操作&#xff0c;比如磁盘的读写&#xff0c;网卡的读写&#xff0c;键盘&#xff0c;鼠标的读入等&#xff0c;大多数应用开发人员使用高级语言进行开发&#xff0c;例如C&#xff0c;C&#xff0c;java&#xff0c;python等&#xff0…

getimg()在java中,java – 使用getClass()加载资源getResource()

我正在尝试加载一个图像作为我的应用程序中的一个图标。根据这个tutorial的适当方法是&#xff1a;protected ImageIcon createImageIcon(String path, String description){java.net.URL imgURL getClass().getResource(path);if (imgURL ! null) {return new ImageIcon(imgU…

Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录

0、目录 整体架构目录&#xff1a;ASP.NET Core分布式项目实战-目录 k8s架构目录&#xff1a;Kubernetes(k8s)集群部署&#xff08;k8s企业级Docker容器集群管理&#xff09;系列目录 一、感谢 在此感谢.net core社区的帮助。感谢。 二、系列部署目录 0、部署环境规划 1、自签T…

每天都用手机,你对麦克风了解吗?

简 介&#xff1a; 通过对于实际驻极体MIC进行拆解&#xff0c;看到其中的结构&#xff0c;对比起工作原理&#xff0c;实在令人难以想象它的工作机制是可行的&#xff0c;尽管现在它已经广泛应用在周围很多电子设备中。关键词&#xff1a; 驻极体&#xff0c;MIC01 驻极体话筒…

网络运维管理的质变

未来网络运维趋势 未来的网络发展趋势可以用三个多样化来概括&#xff0c;一是网络设备的多样化&#xff0c;二是网络组网方式的多样化&#xff0c;三是网络应用的多样化&#xff1b;再加上网络发展与信息化建设的紧密结合&#xff0c;这使得未来的网络运维工作面临着新的…

Java @responsebody,springMVC 使用注解@ResponseBody 不能返回JSON数据

控制器中代码RequestMapping(value "/listArea",method RequestMethod.GET)ResponseBodyprivate Map listArea(){Map modelMap new HashMap<>();List list areaService.getAreaList();modelMap.put("rows", list);modelMap.put("total"…

好了,我不想回深圳了~

国庆节算长假&#xff0c;一共七天&#xff0c;高速免费。如果一个人&#xff0c;待在家里睡上七天&#xff0c;可能我在第二天就会特别无聊&#xff0c;想找事情做&#xff0c;因为国庆离开深圳的人很多&#xff0c;我曾经有一次放假去球场打球&#xff0c;结果很失落&#xf…

开源微信管家平台——JeeWx 捷微4.0 微服务版本发布,全新架构,全新UI,提供强大的图文编辑器...

JeeWx捷微4.0 微服务版本发布^_^ 换代产品&#xff08;全新架构&#xff0c;全新UI&#xff0c;提供强大的图文编辑器&#xff09; JEEWX 从4.0版本开始&#xff0c;技术架构全新换代&#xff0c;采用微服务架构&#xff0c;插件式开发&#xff0c;每个业务模块都是独立的JAR…

手把手教用XNA开发winphone7游戏(三)

XNA Game Studio 游戏循环 在这部分中您将重点两剩余部分的游戏 — — 重写Update 和 Draw 功能。有些大大可能看过相关微软的训练包&#xff0c;我这里主要是帮一些初学者。希望各位大大包含&#xff0c;毕竟文章发出来还是有工作量的。大家觉得有用就好&#xff0c;要是没有耽…