如何获知拖放快捷方式的创建?

时间:2015-07-20 14:00:07

标签: android shortcut launcher

每次创建Homescreen / Launcher上的新快捷方式时,我都会尝试接收活动。我当前的配置如下所示,但如果快捷方式是从菜单中拖放创建的,则我从未收到任何事件

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />    
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<application>
  <receiver  
     android:name=".InstallShortcutReceiver">
    <intent-filter>
      <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
    </intent-filter>
  </receiver>
</application>

我的想法可能吗?

最诚挚的问候,André

1 个答案:

答案 0 :(得分:0)

我的解决方案看起来像。

<?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;
}