服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android通过应用程序创建快捷方式的方法

Android通过应用程序创建快捷方式的方法

2021-04-01 15:46Ruthless Android

这篇文章主要介绍了Android通过应用程序创建快捷方式的方法,涉及Android基于应用程序创建快捷方式的图标及动作等技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了android通过应用程序创建快捷方式的方法。分享给大家供大家参考。具体如下:

android 快捷方式是桌面最基本的组件。它用于直接启动某一应用程序的某个组件。

一般情况下,可以在launcher的应用程序列表上,通过长按某一个应用程序的图标在左面上创建改该应用程序的快捷方式。另外,还可以通过两种方式在桌面上添加快捷方式:

一:在应用程序中创建一个intent,然后以broadcast的形式通知launcher创建一个快捷方式。

二:为应用程序的组件注册某一个符合特定条件的intentfilter,然后可以直接在launcher的桌面添加启动该组件的快捷方式。

下面模拟在应用程序中添加快捷方式

main.xml布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <button android:id="@+id/createshortcut"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:textsize="20px"
  android:text="创建快捷键"/>
 <button android:id="@+id/exit"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:textsize="20px"
  android:text="退出"/>
</linearlayout>

清单文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ljq.action" android:versioncode="1"
 android:versionname="1.0">
 <application android:icon="@drawable/icon"
  android:label="@string/app_name">
  <activity android:name=".shortcutaction"
   android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.main" />
    <category
     android:name="android.intent.category.launcher" />
   </intent-filter>
  </activity>
 </application>
 <uses-sdk android:minsdkversion="7" />
 <!-- 添加快捷键权限 -->
 <uses-permission
  android:name="com.android.launcher.permission.install_shortcut" />
</manifest>

shortcutaction类:

  1. package com.ljq.action; 
  2. import android.app.activity; 
  3. import android.content.intent; 
  4. import android.os.bundle; 
  5. import android.os.parcelable; 
  6. import android.view.view; 
  7. import android.view.view.onclicklistener; 
  8. import android.widget.button; 
  9. /** 
  10.  * 通过应用程序创建快捷方式 
  11.  * 
  12.  * @author jiqinlin 
  13.  * 
  14.  */ 
  15. public class shortcutaction extends activity implements onclicklistener{ 
  16.  private button createshortcut=null//创建快捷键按钮 
  17.  private button exit=null;//退出系统 
  18.  @override 
  19.  public void oncreate(bundle savedinstancestate) { 
  20.   super.oncreate(savedinstancestate); 
  21.   setcontentview(r.layout.main); 
  22.   createshortcut=(button)findviewbyid(r.id.createshortcut); 
  23.   exit=(button)findviewbyid(r.id.exit); 
  24.   createshortcut.setonclicklistener(this); 
  25.   exit.setonclicklistener(this); 
  26.  } 
  27.  public void onclick(view v) { 
  28.   //button btn=(button)v; 
  29.   switch (v.getid()) { 
  30.   case r.id.createshortcut: 
  31.    //string title=getresources().getstring(r.string.title); 
  32.    intent addintent=new intent("com.android.launcher.action.install_shortcut"); 
  33.    parcelable icon=intent.shortcuticonresource.fromcontext(this, r.drawable.png); //获取快捷键的图标 
  34.    intent myintent=new intent(this, shortcutaction.class); 
  35.    addintent.putextra(intent.extra_shortcut_name, "快捷方式");//快捷方式的标题 
  36.    addintent.putextra(intent.extra_shortcut_icon_resource, icon);//快捷方式的图标 
  37.    addintent.putextra(intent.extra_shortcut_intent, myintent);//快捷方式的动作 
  38.    sendbroadcast(addintent);//发送广播 
  39.    break
  40.   case r.id.exit: 
  41.    system.exit(0); 
  42.    break
  43.   } 
  44.  } 

运行结果:

Android通过应用程序创建快捷方式的方法Android通过应用程序创建快捷方式的方法

希望本文所述对大家的android程序设计有所帮助。

延伸 · 阅读

精彩推荐