android 监听 APK 安装 与 删除等过程

这是我项目里需要在安装完应用后,马上能侦听到新的应用安装成功,并且更新相应的界面用到的

1.项目里添加侦听类,然后配置文件加权限,就ok

 

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class getBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
              
                  if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
                    Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();
            }
                else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
                    Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();
            }
             /*   else  if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
                    Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show();
            }*/
                else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
                    Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
            }
               /* else  if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
                    Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show();
            }*/
              /*  else  if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
                    Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show();
            }*/
           
        }
      
}

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="zy.Broadcast"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Broadcast"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
      <receiver android:name="getBroadcast" android:enabled="true" >
         <intent-filter>
          <action android:name="android.intent.action.PACKAGE_ADDED"></action>
          <!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>-->
          <action android:name="android.intent.action.PACKAGE_REMOVED"></action>
          <action android:name="android.intent.action.PACKAGE_REPLACED"></action>
          <!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>-->
        <!--    <action android:name="android.intent.action.PACKAGE_INSTALL"></action>-->
            <data android:scheme="package"></data>
              </intent-filter>
</receiver>
    </application>
    <uses-sdk android:minSdkVersion="7" />
   
</manifest>

 

2.代码实现添加

private final BroadcastReceiver apkInstallListener = new BroadcastReceiver() {  
      
       @Override 
       public void onReceive(Context context, Intent intent) {  
             
    if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
    
    System.out.println("**************Broadcase*************");
    
    File file = uninstallApk.get(isDeleted);
    System.out.println(file.toString()+"*****");
     file.delete();
        //System.out.println(uninstallApk.size()+"(*******"+uApks.size());
     if(uninstallApk!=null&&uApks!=null)
     {
      uninstallApk.remove(isDeleted);
      uApks.remove(isDeleted);
     }
    
        
    
    //清除集合里面的值
        if(uninstallApk!=null)
        {
         System.out.println("onpause******"+uninstallApk.size());
         uninstallApk.clear();
        }
        if(uApks!=null)
        {
         uApks.clear();
        }
            System.out.println("******应用添加***"+isDeleted);
      Toast.makeText(context, "有应用被添加"+isDeleted, Toast.LENGTH_LONG).show();
    }
        else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
            System.out.println("*****应用被删除");
         Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();
    }
     /*   else  if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
            Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show();
    }*/
        else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
            System.out.println("****应用被替换");
         Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
    }
       /* else  if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
            Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show();
    }*/
      /*  else  if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
            Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show();
    }*/
                        
    }
   };  

   // 注册监听  
   private void registerSDCardListener(){  
       IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
       intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);  
       intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);  
       intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);   
       intentFilter.addDataScheme("package");  
       registerReceiver(apkInstallListener, intentFilter);  
   } 

 

 

java里的调用 registerSDCardListener()

@Override
 protected void onDestroy()
 {
  super.onDestroy();
  //unregisterReceiver(apkInstallListener);
 }
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现Android侧滑菜单有多种方式,其中一种比较常见的实现方式是使用DrawerLayout和NavigationView。 步骤如下: 1. 在XML布局文件中添加DrawerLayout和NavigationView,其中NavigationView中可以添加菜单项。 ``` <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 主界面内容 --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 侧滑菜单 --> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header" app:menu="@menu/nav_menu" /> </android.support.v4.widget.DrawerLayout> ``` 2. 在Activity中设置DrawerLayout和NavigationView的监听器,并在onOptionsItemSelected方法中处理菜单项点击事件。 ``` public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; private NavigationView mNavigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDrawerLayout = findViewById(R.id.drawer_layout); mNavigationView = findViewById(R.id.navigation_view); mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { // 处理菜单项点击事件 return false; } }); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); mDrawerLayout.addDrawerListener(toggle); toggle.syncState(); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { mDrawerLayout.openDrawer(GravityCompat.START); return true; } return super.onOptionsItemSelected(item); } } ``` 3. 在NavigationView中添加菜单项,并为菜单项设置图标和标题。 ``` <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_home" android:title="Home" /> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_gallery" android:title="Gallery" /> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_slideshow" android:title="Slideshow" /> </group> </menu> ``` 至此,实现了一个简单的Android侧滑菜单。如果要实现仿QQ侧滑删除功能,可以在ListView或RecyclerView中添加滑动删除的功能,并在删除时更新侧滑菜单中的未读消息数等信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值