HardBirch

android快捷方式(shortCut)创建和判断存在与否的操作

时间:13-01-30 栏目:android学习笔记 作者:魔豆先生 评论:0 点击: 11,515 次

本文由魔豆博主(蒋冬冬)原创 编译,转载请注明出处和链接!谢谢!

   关于Launcher的快捷方式信息是 存放在 com.android.launcher的launcher.db的favorites表 中的,所以,创建,查询,删除动作都需要操作到那张表。

android应用程序安装完成后是不会自动创建快捷方式的,所以可以自己在程序启动时实现。方法如下:

创建shortcut Activity的intent属性声明:

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

 

查询快捷方式 manifest.xml文件中 权限和Intent属性声明

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shao.add"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MythroadActivity"
android:label="@string/app_name">
<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>

如何判断快捷方式是否已经创建?可以查询此表得到信息。分享示例代码如下:

import android.app.*;

public class AddShortCutActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
boolean existed =checkShortCut();//如果已经创建,则不需要在创建
if( existed == false){
createShortCut();
}
}
public void createShortCut(){
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 设置属性
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(AddShortCutActivity.this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);

// 是否允许重复创建
shortcut.putExtra("duplicate", false);
Intent intent = new Intent(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);
}
public boolean checkShortCut( ){
boolean isInstallShortcut = false ;
final ContentResolver cr = this.getContentResolver();
 final String AUTHORITY = "com.android.launcher.settings";
final Uri CONTENT_URI = Uri.parse("content://" +
AUTHORITY + "/favorites?notify=true");
Cursor c = cr.query(CONTENT_URI,
new String[] {"title","iconResource" },
"title=?",
new String[] {getString(R.string.app_name ) }, null);
if(c!=null && c.getCount()>0){
System.out.println("已创建");

return true;
}
return false ;
}
}

问题一:如果直接使用的话部分手机是不能成功的,为什么呢?原因是系统版本的差异造成的。

分析与解决: android 2.2版本和android1.5版本的launcher中的清单文件

1.5的如下:

Java代码
<!-- Intent received used to install shortcuts from other applications -->
<receiver
android:name=".InstallShortcutReceiver"
android:permission="com.lp.launcher.permission.INSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.lp.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>
</receiver>

<!-- Intent received used to uninstall shortcuts from other applications -->
<receiver
android:name=".UninstallShortcutReceiver"
android:permission="com.lp.launcher.permission.UNINSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.lp.launcher.action.UNINSTALL_SHORTCUT" />
</intent-filter>
</receiver>

<!-- The settings provider contains Home's data, like the workspace favorites -->
<provider
android:name="LauncherProvider"
android:authorities="com.lp.launcher.settings"
android:writePermission="com.lp.launcher.permission.WRITE_SETTINGS"
android:readPermission="com.lp.launcher.permission.READ_SETTINGS" />

 
2.2的如下:

Java代码
<!-- Intent received used to install shortcuts from other applications -->
<receiver
android:name="com.android.launcher2.InstallShortcutReceiver"
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>
</receiver>

<!-- Intent received used to uninstall shortcuts from other applications -->
<receiver
android:name="com.android.launcher2.UninstallShortcutReceiver"
android:permission="com.android.launcher.permission.UNINSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.UNINSTALL_SHORTCUT" />
</intent-filter>
</receiver>

<!-- The settings provider contains Home's data, like the workspace favorites -->
<provider
android:name="com.android.launcher2.LauncherProvider"
android:authorities="com.android.launcher2.settings"
android:writePermission="com.android.launcher.permission.WRITE_SETTINGS"
android:readPermission="com.android.launcher.permission.READ_SETTINGS" />

可以看出来 创建和删除快捷方式的receiver没什么变化,provider有了变化:

Java代码
android:authorities="com.android.launcher.settings"

Java代码
android:authorities="com.android.launcher2.settings"

所以用在2.2之后的版本是无效的,需要修改下字串。

 

问题二:可能广大的网友 还会发现还会有部分手机判断是否存在快捷方式的时候信息还是不准确。会出现"Failed to find provider info for com.android.calendar"的error,既不是launcher也不是launcher2,那到底哪里出现了问题了呢?

 解决方法:在国外论坛里看到也有同样的情况出现,有人这样解释的:

The launcher is an Application under the Handset Manufacturer responsibility. The Authority is then not always "com.android.launcher2.settings". The Handset Manufacturer may rewrite its own. It can be "com.android.twlauncher" or anything else depending on the Java package.

You need to retrieve the right authority by searching for a provider that declares the read/write permissions "com.android.launcher.permission.READ_SETTINGS" or "com.android.launcher.permission.WRITE_SETTINGS".

所以,我们需要去查询provider获取真实的content的uri前缀,这样才能保证查询的时候匹配成功。提供获取真实字串的函数如下:

OK,再在上述checkShortCut的函数中,调用该函数获取authority字串就OK啦~

声明: 本文由( 魔豆先生 )原创编译,转载请保留链接: android快捷方式(shortCut)创建和判断存在与否的操作

android快捷方式(shortCut)创建和判断存在与否的操作:等您坐沙发呢!

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐