Android Technology Point notes-(Install app and uninstall app broadcast)

Source: Internet
Author: User

Android Technology Point notes-(Install app and uninstall app broadcast)


Summary: Summarize the broadcast usage of apps installed and uninstalled in Android.

On Android, the installation and uninstallation will send out the corresponding broadcasts, and the system will broadcast Android.intent.action.PACKAGE_ADDED after the application installation is complete.

The installed package name can be obtained by intent.getdatastring (). The system sends Android.intent.action.PACKAGE_REMOVED broadcasts when the program is uninstalled. the same intent.getdatastring () gets the package name that was unloaded. According to the actual needs to do the corresponding operation.


Example:

Create a new Broadcastreceiver class, accept the broadcast and process it as required.

public class Myreceiver extends Broadcastreceiver {@Override public void OnReceive (Context Co  ntext, Intent Intent) {//Receive broadcast: Run Program if (Intent.getaction (). Equals ("Android.intent.action.BOOT_COMPLETED") after system boot is complete                     {Intent newintent = new Intent (context, xxxactivity.class);                        Newintent.setaction ("Android.intent.action.MAIN");                      Newintent.addcategory ("Android.intent.category.LAUNCHER");                        Newintent.setflags (Intent.flag_activity_new_task);                Context.startactivity (newintent); }//Receive broadcast: Automatically start the new installation application after a new application package is installed on the device if (Intent.getaction (). Equals ("Android.intent.action.PACKAGE_ADDED")) {  & nbsp                String PackageName = intent.getdatastring (). substring (8);                    SYSTEM.OUT.PRINTLN ("---------------" + PackageName);     &nbsp       Intent newintent = new Intent ();                    Newintent.setclassname (packagename,packagename+). Mainactivity ");                  newintent.setaction   ("Android.intent.action.MAIN");                             Newintent.addcatego Ry ("Android.intent.category.LAUNCHER");                             Newintent.setflags ( Intent.flag_activity_new_task);                    context.startactivity (newintent);               }               //Receive broadcast: on device An application package was deleted.        if (Intent.getaction () equals ("Android.intent.action.PACKAGE_REMOVED")) {                   SYSTEM.OUT.PRINTLN ("********************************");                    Databasehelper dbhelper = new Databasehelper ();                    Dbhelper.executesql ("delete from xxx");               }}

Note:Modifying the Androidmanifest.xml configuration file

<?xml version= "1.0" encoding= "UTF-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" App.ui "> <application> <receiver android:name=".                       Myreceiver "android:label=" @string/app_name "> <intent-filter> <action android:name= "Android.intent.action.BOOT_COMPLETED"/> <category androi                    D:name= "Android.intent.category.LAUNCHER"/> </intent-filter> <intent-filter> <action android:name= "Android.intent.action.PACKAGE_ADDED"/> <action Andro Id:name= "Android.intent.action.PACKAGE_REMOVED"/> <data android:scheme= "package"/> &lt ;! --Attention!! This sentence must be added, otherwise not receive broadcast--</intent-filter> </receiver> <activity A Ndroid:name= ". Xxxactivity" Android:labEl= "XXX" > <intent-filter> <action android:name= "android.intent.action. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </ intent-filter> </activity> </application> <uses-permission android:name= "Android.per Mission. INTERNET "/> <uses-permission android:name=" Android.permission.RESTART_PACKAGES "/> <uses-p Ermission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/> </manifest>




Android Technology Point notes-(Install app and uninstall app broadcast)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.