Android technology notes-(install and uninstall an application broadcast)

Source: Internet
Author: User

Android technology notes-(install and uninstall an application broadcast)

Android technology notes-(install and uninstall an application broadcast)

 

This article summarizes the broadcast usage of installing and uninstalling apps in Android.
 

In the android system, the corresponding broadcast is sent during installation and uninstallation. After the application is installed, the android. intent. action. PACKAGE_ADDED broadcast is sent.

You can use intent. getDataString () to obtain the package name. When the program is uninstalled, the system sends android. intent. action. PACKAGE_REMOVED broadcast. Similarly, intent. getDataString () obtains the name of the uninstalled package. You can perform operations as needed.

 

Example:

Create a new BroadcastReceiver class, accept the corresponding broadcast, and handle it as needed.

 

Public class MyReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {// receive broadcast: run the program if (intent. getAction (). equals ("android. intent. action. BOOT_COMPLETED ") {Intent newIntent = new Intent (context, xxxActivity. class); newIntent. setAction ("android. intent. action. MAIN "); newIntent. addCategory ("android. intent. category. LAUNCHER "); newIntent. setFlags (Inte Nt. FLAG_ACTIVITY_NEW_TASK); context. startActivity (newIntent);} // receives broadcast: after an application package is installed on the device, 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 +. mainActivity "); newIntent. setAction (" Ndroid. intent. action. MAIN "); newIntent. addCategory ("android. intent. category. LAUNCHER "); newIntent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); context. startActivity (newIntent);} // receives broadcast: An application package is deleted from the device. If (intent. getAction (). equals ("android. intent. action. PACKAGE_REMOVED ") {System. out. println ("********************************"); databaseHelper dbhelper = new DatabaseHelper (); dbhelper.exe cuteSql ("delete from xxx ");}}

Note:Modify the AndroidManifest. xml configuration file

 

 

        
                    
                     
                                                 
                       
    
               
                                                               
           
                      
                  
                             
                                                
                      
   
                         
             
              
           
    
   
  
 


 

 

 

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.