編輯:關於 Android 程式設計
應用希望安裝後能在桌面上自動放一個啟動圖示,做法是發送一個 com.android.launcher.action.INSTALL_SHORTCUT 廣播。不過直接無條件發送會導致每次啟動都新增一個圖示,所以還需要先判斷桌面上是否已存在。
需要說明的是,這種方式適用於 Android 原生系統與大部分非原生系統;部分深度定製的桌面(Launcher)可能修改了協議或乾脆忽略該廣播,實測時要多找幾台機型驗證。
<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" />
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 原生系統和非原生系統
*/
public class MainActivity extends Activity {
@Override
protected 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="https://schemas.android.com/apk/res/android"
xmlns:tools="https://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>
getAuthorityFromPermission 是整個去重邏輯的關鍵。由於不同廠商桌面的 READ_SETTINGS 權限對應的 ContentProvider authority 並不固定,程式需要先遍歷系統已安裝套件,找出哪個 Provider 宣告了這個權限,再拿它的 authority 去拼出 content://authority/favorites 這個 URI。查詢時以應用名稱作為條件比對 title,一旦有結果就說明快捷方式已存在,直接跳過建立。
實務上要留意兩點:查詢完的 Cursor 記得關閉以免洩漏;另外使用者是可以手動把快捷方式刪掉的,因此更嚴謹的做法是比對 title 的同時也驗證 icon 與 intent 是否一致。
Android Volley 網路框架全面解析與原始碼剖析
Volley 簡介平時開發 Android 應用時,幾乎都離不開網路技術,而大多數情況下應用都會採用 HTTP 協定來傳送和接收資料。Android 系統主要提供了 Ht
把微信支付與支付寶的服務端操作放到客戶端實作
前言 現在的 App 大多需要接入支付功能,而主流方案就是微信支付與支付寶支付。網上相關資料很多,但隨著官方不斷變動,部分資料可能早已失效,因此建議直接看官方文件(微信開
Android 筆記(十一):深入理解 ListView 與 Adapter
一、ListView 概覽 ListView 是 Android 開發中相當常見的元件,它以列表形式呈現資料。一般來說,一個 ListView 由三個部分組成: 1. V
Android 開發架構 MVP 模式的思考與實踐
什麼是 MVP? View 層:負責展示數據並對用戶行為做出反饋。在 Android 平台上,它可以對應 Activity、Fragment、View 或對話框。 Mo