android案頭捷徑添加 刪除 更新 自動運行

來源:互聯網
上載者:User

添加捷徑  <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>  

   Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

       shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));

       shortcutIntent.putExtra("duplicate", false);

       Intent intent = new Intent();

       intent.setComponent(new ComponentName(getPackageName(), MainActivity.class.getName()));

       shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

       shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));

      sendBroadcast(shortcutIntent);

public class BootReceiver extends BroadcastReceiver {   

    public void onReceive(Context context, Intent intent) {  

        //接收廣播:系統啟動完成後運行程式   

        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {   

             Intent newIntent = new Intent(context, searchDemo.class);   

             newIntent.setAction("android.intent.action.MAIN");        

             newIntent.addCategory("android.intent.category.LAUNCHER");       

             newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);           

             context.startActivity(newIntent);   

        }   

        //接收廣播:裝置上新安裝了一個應用程式套件組合後自動啟動新安裝應用程式。   

        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {   

            String packageName = intent.getDataString().substring(8);   

            System.out.println("---------------" + packageName);   

            Intent newIntent = new Intent();   

           newIntent.setClassName(packageName,packageName+ ".searchDemo");

           newIntent.setAction("android.intent.action.MAIN");     

           newIntent.addCategory("android.intent.category.LAUNCHER");            

           newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   

           context.startActivity(newIntent);   

        }   

 //接收廣播:裝置上刪除了一個應用程式套件組合。   :這個貌似需要root許可權

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>  

        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) { 

       String ACTION_UNINSTALL_SHORTCUT ="com.android.launcher.action.UNINSTALL_SHORTCUT";   

     Intent intent3 = new Intent(ACTION_UNINSTALL_SHORTCUT );   

      intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, R.string.app_name);   

     // 要刪除的應用程式的ComponentName,即應用程式套件組合名+activity的名字   

     ComponentName comp = new ComponentName("搜尋","searchDemo");   

      intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent()   

      .setComponent(comp).setAction("android.intent.action.MAIN"));   

     context.sendBroadcast(intent);  

        }   

    }  

為了您的安全,請只開啟來源可靠的網址

來自: http://hi.baidu.com/lfcomputer/blog/item/15c31a6649bb6a37ab184c9b.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.