我的解決方案看起來像。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="paul.pweb" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".OnBootReceiver"
android:enabled="true"
android:exported="true"
android:label="OnBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>
public class OnBootReceiver extends BroadcastReceiver {
private class CheckShortcutsTask extends TimerTask {
private final Context context;
public CheckShortcutsTask(Context context) {
this.context = context;
}
@Override
public void run() {
try {
new Utils().checkShortcuts(context);
} catch(Exception e) {
Log.e("pweb", "Exception in " + CheckShortcutsTask.class, e);
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
Timer timer = new Timer();
timer.schedule(new CheckShortcutsTask(context), 0, 1000);
}
}
public List<Shortcut> getShortcuts(ContentResolver resolver) throws Exception {
String url;
if (Build.VERSION.SDK_INT <8) {
url = "content://com.android.launcher.settings/favorites?Notify=true";
} else {
url = "content://com.android.launcher2.settings/favorites?Notify=true";
}
Cursor cursor = resolver.query(Uri.parse(url), null, null, null, null);
List<Shortcut> shortcuts = new ArrayList<>();
while(cursor.moveToNext()) {
Shortcut shortcut = new Shortcut();
int i = cursor.getColumnIndex("title");
if(i < 0)
continue;
shortcut.name = cursor.getString(i);
i = cursor.getColumnIndex("intent");
if(i < 0)
continue;
String s = cursor.getString(i);
if(s == null)
continue;
shortcut.intent = Intent.parseUri(s, 0);
shortcuts.add(shortcut);
}
cursor.close();
return shortcuts;
}