Android案頭捷徑的設定(一)

來源:互聯網
上載者:User

package cn.testshortcut;import java.util.List;import android.net.Uri;import android.os.Bundle;import android.text.TextUtils;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.content.pm.ProviderInfo;import android.content.pm.PackageManager.NameNotFoundException;import android.database.Cursor;/** * Demo描述: * 案頭捷徑的設定 *  * 備忘說明: * 該方式只適合Android原生系統和非原生系統 *  * 使用權限設定: * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> * <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> * <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> */public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);addShortcutToDesktop(MainActivity.this);}public static void addShortcutToDesktop(Context context) {if (!hasShortcut(context)) {Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");shortcutIntent.putExtra("duplicate", false);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context,R.drawable.ic_launcher));Intent intent = new Intent(context, MainActivity.class);intent.setAction("android.intent.action.MAIN");intent.addCategory("android.intent.category.LAUNCHER");shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);context.sendBroadcast(shortcutIntent);}}private static boolean hasShortcut(Context context) {String AUTHORITY = getAuthorityFromPermission(context,"com.android.launcher.permission.READ_SETTINGS");System.out.println(" AUTHORITY ..." + AUTHORITY);if (AUTHORITY == null) {return false;}Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY+ "/favorites?notify=true");String title = "";final PackageManager packageManager = context.getPackageManager();try {title = packageManager.getApplicationLabel(packageManager.getApplicationInfo(context.getPackageName(),PackageManager.GET_META_DATA)).toString();} catch (NameNotFoundException e) {        e.printStackTrace();}Cursor c = context.getContentResolver().   query(CONTENT_URI,new String[] { "title" }, "title=?", new String[] { title },null);if (c != null && c.getCount() > 0) {return true;}return false;}private static String getAuthorityFromPermission(Context context,String permission) {if (TextUtils.isEmpty(permission)) {return null;}List<PackageInfo> packageInfoList = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);if (packageInfoList == null) {return null;}for (PackageInfo packageInfo : packageInfoList) {ProviderInfo[] providerInfos = packageInfo.providers;if (providerInfos != null) {for (ProviderInfo providerInfo : providerInfos) {if (permission.equals(providerInfo.readPermission)|| permission.equals(providerInfo.writePermission)) {return providerInfo.authority;}}}}return null;}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"     >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="建立案頭捷徑"        android:layout_centerInParent="true"     /></RelativeLayout>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.