2015-07-20 59 views
0

我每次嘗試在主屏幕/啓動器上創建一個新的快捷方式時收到一個事件。我目前的配置如下,但如果從菜單中拖放創建快捷方式,我永遠不會收到事件如何獲取有關拖放快捷方式創建的信息?

<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é

回答

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