Android之ShortCut[Broadcast方式]
16lz
2021-01-23
Android之ShortCut[Broadcast方式] 在Android中创建ShortCut大概有两种方法。
第一种方法就是参照api demos中写的那个,通过设置setResult(RESULT_OK, intent);来创建ShortCut,这种方式在稍后分析。
本文以Broadcast方式方式来介绍Android中ShortCut的创建。
在创建或删除ShortCut的时候先需要在AndroidManifest.xml中增加两个权限
<!-- 创建桌面快捷方式的权限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
另外记得在创建或删除ShortCut的Intent中设置Action为
com.android.launcher.action.INSTALL_SHORTCUT(创建)
com.android.launcher.action.UNINSTALL_SHORTCUT(删除)
这样发送出去的广播才能被Android系统接受到
详细的代码: package com.zhy.shortcut;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ShortCutActivity extends Activity{
private static final StringCREATE_SHORTCUT_ACTION = " com.android.launcher.action.INSTALL_SHORTCUT " ;
private static final StringDROP_SHORTCUT_ACTION = " com.android.launcher.action.UNINSTALL_SHORTCUT " ;
private static final StringPREFERENCE_KEY_SHORTCUT_EXISTS = " IsShortCutExists " ;
Buttonbutton;
// 获取默认的SharedPreferences
SharedPreferencessharedPreferences;
// 从SharedPreferences获取是否存在快捷方式若不存在返回false程序第一次进来肯定返回false
boolean exists;
@Override
public void onCreate(BundlesavedInstanceState){
super .onCreate(savedInstanceState);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences( this );
exists = sharedPreferences.getBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, false );
// 创建桌面快捷方式
// 若第一次启动则创建,下次启动则不创建
if ( ! exists){
setUpShortCut();
}
setContentView(R.layout.main);
button = (Button)findViewById(R.id.dropShortCut);
button.setOnClickListener( new OnClickListener(){
@Override
public void onClick(Viewv){
tearDownShortCut();
}
});
}
/**
*创建桌面快捷方式
*/
private void setUpShortCut(){
Intentintent = new Intent(CREATE_SHORTCUT_ACTION);
// 设置快捷方式图片
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext( this ,R.drawable.logo));
// 设置快捷方式名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, " sina " );
// 设置是否允许重复创建快捷方式false表示不允许
intent.putExtra( " duplicate " , false );
// 设置快捷方式要打开的intent
// 第一种方法创建快捷方式要打开的目标intent
IntenttargetIntent = new Intent();
// 设置应用程序卸载时同时也删除桌面快捷方式
targetIntent.setAction(Intent.ACTION_MAIN);
targetIntent.addCategory( " android.intent.category.LAUNCHER " );
ComponentNamecomponentName = new ComponentName(getPackageName(), this .getClass().getName());
targetIntent.setComponent(componentName);
// 第二种方法创建快捷方式要打开的目标intent
/*
*Intent
*targetIntent=getPackageManager().getLaunchIntentForPackage(getPackageName
*());
*/
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,targetIntent);
// 发送广播
sendBroadcast(intent);
Editoreditor = sharedPreferences.edit();
editor.putBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, true );
editor.commit();
}
/**
*删除桌面快捷方式
*/
private void tearDownShortCut(){
Intentintent = new Intent(DROP_SHORTCUT_ACTION);
// 指定要删除的shortcut名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, " sina " );
StringappClass = getPackageName() + " . " + this .getLocalClassName();
ComponentNamecomponent = new ComponentName(getPackageName(),appClass);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setAction(Intent.ACTION_MAIN).setComponent(component));
sendBroadcast(intent);
}
} AndroidManifest.xml <? xmlversion="1.0"encoding="utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="com.zhy.shortcut"
android:versionCode ="1"
android:versionName ="1.0" >
< uses-sdk android:minSdkVersion ="8" />
<!-- 创建桌面快捷方式的权限 -->
< uses-permission android:name ="com.android.launcher.permission.INSTALL_SHORTCUT" />
< uses-permission android:name ="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
< application
android:icon ="@drawable/ic_launcher"
android:label ="@string/app_name" >
< activity
android:label ="@string/app_name"
android:name =".ShortCutActivity" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
</ application >
</ manifest >
注意:该方式创建的ShortCut会在启动应用的时候就创建ShortCut。下一讲中的创建方式,仅仅只是在长按Android桌面后你的当前应用创建的快捷方式可以在这里检索到,需要你手动创建出来。
第一种方法就是参照api demos中写的那个,通过设置setResult(RESULT_OK, intent);来创建ShortCut,这种方式在稍后分析。
本文以Broadcast方式方式来介绍Android中ShortCut的创建。
在创建或删除ShortCut的时候先需要在AndroidManifest.xml中增加两个权限
<!-- 创建桌面快捷方式的权限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
另外记得在创建或删除ShortCut的Intent中设置Action为
com.android.launcher.action.INSTALL_SHORTCUT(创建)
com.android.launcher.action.UNINSTALL_SHORTCUT(删除)
这样发送出去的广播才能被Android系统接受到
详细的代码: package com.zhy.shortcut;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ShortCutActivity extends Activity{
private static final StringCREATE_SHORTCUT_ACTION = " com.android.launcher.action.INSTALL_SHORTCUT " ;
private static final StringDROP_SHORTCUT_ACTION = " com.android.launcher.action.UNINSTALL_SHORTCUT " ;
private static final StringPREFERENCE_KEY_SHORTCUT_EXISTS = " IsShortCutExists " ;
Buttonbutton;
// 获取默认的SharedPreferences
SharedPreferencessharedPreferences;
// 从SharedPreferences获取是否存在快捷方式若不存在返回false程序第一次进来肯定返回false
boolean exists;
@Override
public void onCreate(BundlesavedInstanceState){
super .onCreate(savedInstanceState);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences( this );
exists = sharedPreferences.getBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, false );
// 创建桌面快捷方式
// 若第一次启动则创建,下次启动则不创建
if ( ! exists){
setUpShortCut();
}
setContentView(R.layout.main);
button = (Button)findViewById(R.id.dropShortCut);
button.setOnClickListener( new OnClickListener(){
@Override
public void onClick(Viewv){
tearDownShortCut();
}
});
}
/**
*创建桌面快捷方式
*/
private void setUpShortCut(){
Intentintent = new Intent(CREATE_SHORTCUT_ACTION);
// 设置快捷方式图片
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext( this ,R.drawable.logo));
// 设置快捷方式名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, " sina " );
// 设置是否允许重复创建快捷方式false表示不允许
intent.putExtra( " duplicate " , false );
// 设置快捷方式要打开的intent
// 第一种方法创建快捷方式要打开的目标intent
IntenttargetIntent = new Intent();
// 设置应用程序卸载时同时也删除桌面快捷方式
targetIntent.setAction(Intent.ACTION_MAIN);
targetIntent.addCategory( " android.intent.category.LAUNCHER " );
ComponentNamecomponentName = new ComponentName(getPackageName(), this .getClass().getName());
targetIntent.setComponent(componentName);
// 第二种方法创建快捷方式要打开的目标intent
/*
*Intent
*targetIntent=getPackageManager().getLaunchIntentForPackage(getPackageName
*());
*/
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,targetIntent);
// 发送广播
sendBroadcast(intent);
Editoreditor = sharedPreferences.edit();
editor.putBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, true );
editor.commit();
}
/**
*删除桌面快捷方式
*/
private void tearDownShortCut(){
Intentintent = new Intent(DROP_SHORTCUT_ACTION);
// 指定要删除的shortcut名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, " sina " );
StringappClass = getPackageName() + " . " + this .getLocalClassName();
ComponentNamecomponent = new ComponentName(getPackageName(),appClass);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setAction(Intent.ACTION_MAIN).setComponent(component));
sendBroadcast(intent);
}
} AndroidManifest.xml <? xmlversion="1.0"encoding="utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="com.zhy.shortcut"
android:versionCode ="1"
android:versionName ="1.0" >
< uses-sdk android:minSdkVersion ="8" />
<!-- 创建桌面快捷方式的权限 -->
< uses-permission android:name ="com.android.launcher.permission.INSTALL_SHORTCUT" />
< uses-permission android:name ="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
< application
android:icon ="@drawable/ic_launcher"
android:label ="@string/app_name" >
< activity
android:label ="@string/app_name"
android:name =".ShortCutActivity" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
</ application >
</ manifest >
注意:该方式创建的ShortCut会在启动应用的时候就创建ShortCut。下一讲中的创建方式,仅仅只是在长按Android桌面后你的当前应用创建的快捷方式可以在这里检索到,需要你手动创建出来。
更多相关文章
- android中使用URL Scheme方式启动app
- android中解析文件的三种方式
- Android存储数据的三种方式
- 无废话Android之android下junit测试框架配置、保存文件到手机内
- Android安全机制(2) Android Permission权限控制机制
- Android XML解析学习——Dom方式
- Android 使用OpenCV的三种方式(Android Studio)
- Android M 新的运行时权限开发者需要知道的一切