Android之快捷方式
16lz
2021-12-04
Android之快捷方式 package com.shortcut;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
public class AddShortCutActivity extends Activity {
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
booleanflag=IfaddShortCut();//如果已经创建,则不需要在创建
if(flag==false){
addShortCut();
}
}
publicvoidaddShortCut(){
Intentshortcut=newIntent("com.android.launcher.action.INSTALL_SHORTCUT");
//设置属性
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,getResources().getString(R.string.app_name));
ShortcutIconResourceiconRes=Intent.ShortcutIconResource.fromContext(AddShortCutActivity.this,R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);
//是否允许重复创建
shortcut.putExtra("duplicate",false);
Intentintent=newIntent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(AddShortCutActivity.this,AddShortCutActivity.class);
//设置启动程序
System.out.println("createIcon");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
AddShortCutActivity.this.sendBroadcast(shortcut);
}
publicbooleanIfaddShortCut(){
booleanisInstallShortcut=false;
finalContentResolvercr=AddShortCutActivity.this.getContentResolver();
//本人的2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings"
finalStringAUTHORITY="com.android.launcher2.settings";
finalUriCONTENT_URI=Uri.parse("content://"+AUTHORITY+"/favorites?notify=true");
Cursorc=cr.query(CONTENT_URI,
newString[]{"title","iconResource"},"title=?",
newString[]{getString(R.string.app_name)},null);
if(c!=null&&c.getCount()>0){
isInstallShortcut=true;
}
returnisInstallShortcut;
}
} <? xmlversion="1.0"encoding="utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="com.shortcut"
android:versionCode ="1"
android:versionName ="1.0" >
< uses-sdk android:minSdkVersion ="8" />
< application
android:icon ="@drawable/ic_launcher"
android:label ="@string/app_name" >
< activity
android:label ="@string/app_name"
android:name =".AddShortCutActivity" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
< intent-filter >
< action android:name ="android.intent.action.CREATE_SHORTCUT" ></ action >
</ intent-filter >
</ activity >
</ application >
< uses-permission android:name ="com.android.launcher.permission.READ_SETTINGS" />
< uses-permission android:name ="com.android.launcher.permission.INSTALL_SHORTCUT" />
</ manifest > Android Shortcut操作(一)
http://www.eoeandroid.com/thread-92125-1-1.html
Android Shortcut操作(二)
http://www.eoeandroid.com/thread-92128-1-1.html
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
public class AddShortCutActivity extends Activity {
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
booleanflag=IfaddShortCut();//如果已经创建,则不需要在创建
if(flag==false){
addShortCut();
}
}
publicvoidaddShortCut(){
Intentshortcut=newIntent("com.android.launcher.action.INSTALL_SHORTCUT");
//设置属性
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,getResources().getString(R.string.app_name));
ShortcutIconResourceiconRes=Intent.ShortcutIconResource.fromContext(AddShortCutActivity.this,R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);
//是否允许重复创建
shortcut.putExtra("duplicate",false);
Intentintent=newIntent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(AddShortCutActivity.this,AddShortCutActivity.class);
//设置启动程序
System.out.println("createIcon");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
AddShortCutActivity.this.sendBroadcast(shortcut);
}
publicbooleanIfaddShortCut(){
booleanisInstallShortcut=false;
finalContentResolvercr=AddShortCutActivity.this.getContentResolver();
//本人的2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings"
finalStringAUTHORITY="com.android.launcher2.settings";
finalUriCONTENT_URI=Uri.parse("content://"+AUTHORITY+"/favorites?notify=true");
Cursorc=cr.query(CONTENT_URI,
newString[]{"title","iconResource"},"title=?",
newString[]{getString(R.string.app_name)},null);
if(c!=null&&c.getCount()>0){
isInstallShortcut=true;
}
returnisInstallShortcut;
}
} <? xmlversion="1.0"encoding="utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="com.shortcut"
android:versionCode ="1"
android:versionName ="1.0" >
< uses-sdk android:minSdkVersion ="8" />
< application
android:icon ="@drawable/ic_launcher"
android:label ="@string/app_name" >
< activity
android:label ="@string/app_name"
android:name =".AddShortCutActivity" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
< intent-filter >
< action android:name ="android.intent.action.CREATE_SHORTCUT" ></ action >
</ intent-filter >
</ activity >
</ application >
< uses-permission android:name ="com.android.launcher.permission.READ_SETTINGS" />
< uses-permission android:name ="com.android.launcher.permission.INSTALL_SHORTCUT" />
</ manifest > Android Shortcut操作(一)
http://www.eoeandroid.com/thread-92125-1-1.html
Android Shortcut操作(二)
http://www.eoeandroid.com/thread-92128-1-1.html
更多相关文章
- Android(安卓)权限全集
- Android(安卓)之 Gallery
- Android为HttpClient设置证书(用户名和密码)
- TextInputLayout,让输入框更有灵性
- Android(安卓)常用的SDCARD和内存操作
- Android(安卓)之 ProgressDialog用法介绍
- Android(安卓)封装http请求的工具类
- Android(安卓)删除SD卡文件和文件及创建文件夹和文件
- Android(安卓)SQLite使用