android桌面快捷方式添加 删除 更新 自动运行

添加快捷方式  <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);  

 

        }   

    }  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值