一开始是在service里面写的代码,在service里面采用广播来监听应用安装的广播。
package com.chinachip.customrequirements;
import android.app.AlarmManager;
import android.app.LightManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.BatteryManager;
import android.os.IBinder;
import android.app.INotificationManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.ServiceManager;
import android.os.Handler;
import android.util.Log;
import java.util.List;
/**
* Created by gyxie on 2019/11/21
*/
public class CustomSystemService extends Service {
private static int mLastLevel;
private static int mLastStatus;
private ConnectivityManager connManager;
private LightManager mLightManager;
private AlarmManager mAlarmManager;
private boolean isRegister = false;
public static final String ACTION_AIRPLANE_MODE = "com.chinachip.action.AIRPLANE";
public static final String IS_NOT_FIRST_BOOT = "is_not_frist_boot";
//灭屏10分钟开启飞行模式
private static final long INTERVAL_RESCHEDUAL = 10 * 60 * 1000;
private boolean originAirplane;
private boolean isFirstBoot = false;
public class InstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Tim_A","receive the action=" + action);
//安装广播
if (action.equals(Intent.ACTION_PACKAGE_ADDED) || action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
final String packageName = intent.getDataString();
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
//disableAppNotification(packageName);
}
},10000);
}
}
}
InstallReceiver mInstallReceiver = new InstallReceiver();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(ACTION_AIRPLANE_MODE);
registerReceiver(mBatteryReceiver, filter);
isRegister = true;
/*IntentFilter installFilter = new IntentFilter();
installFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
installFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
installFilter.addDataScheme("package");
registerReceiver(mInstallReceiver, installFilter);*/
//List packages = getPackageManager().getInstalledPackages(0);
isFirstBoot = !Utils.getBooleanPref(this,IS_NOT_FIRST_BOOT);
if(isFirstBoot){
disableAppsNotification();
Utils.saveBooleanPref(this,IS_NOT_FIRST_BOOT,true);
}
}
private void disableAppsNotification(){
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
for(ResolveInfo app:mApps){
String pkg = app.activityInfo.packageName;
if(isPkgWhiteList(pkg)){
continue;
}
disableAppNotification(pkg);
}
}
private boolean isPkgWhiteList(String pkg){
if(pkg.equals("com.android.dialer")
|| pkg.equals("com.android.mms")
|| pkg.equals("com.android.deskclock")){
return true;
}
return false;
}
private void disableAppNotification(String packagename){
PackageManager mPm = getPackageManager();
INotificationManager mNotificationManager = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
// Only the owner can see all apps.
/*int adminRetrieveFlags = PackageManager.MATCH_ANY_USER |
PackageManager.MATCH_DISABLED_COMPONENTS |
PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS;*/
try {
ApplicationInfo info = mPm.getApplicationInfo(packagename,0);
Log.i("Tim_A","CSS disable app pkg.uid="+ info.uid);
int uid = mPm.getPackageUid(packagename, 0);
Log.i("Tim_A","getPackageUid disable app pkg="+ packagename+",uid="+uid);
mNotificationManager.setNotificationsEnabledForPackage(packagename, uid, false);
} catch (Exception e) {
Log.w("Tim_A", "Error calling NoMan", e);
}
}
.....
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Tim_A","intent=" + intent);
connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
mLightManager = (LightManager) getSystemService(Context.LIGHT_SERVICE);
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
originAirplane = Utils.isAirplaneModeOn(this);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (isRegister) {
unregisterReceiver(mBatteryReceiver);
isRegister = false;
}
unregisterReceiver(mInstallReceiver);
}
}
遇到的问题这样子的,每次开机service里面都是可以获取应用的uid的,但是监听那应用安装的广播却获取不到刚安装应用的uid,折腾了一下午,没招,最后只能在PMS里面实现相关逻辑。
diff --git a/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java b/frameworks/base/services/core/java/com/android/server/pm/PackageManagerSe
index 440e582..315b27d 100755
--- a/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -122,6 +122,7 @@ import android.app.ResourcesManager;
import android.app.admin.IDevicePolicyManager;
import android.app.admin.SecurityLog;
import android.app.backup.IBackupManager;
+import android.app.INotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
@@ -19088,10 +19089,25 @@ public class PackageManagerService extends IPackageManager.Stub
if (res.returnCode == PackageManager.INSTALL_SUCCEEDED) {
updateSequenceNumberLP(ps, res.newUsers);
updateInstantAppInstallerLocked(pkgName);
+ //disable the third part apk notification
+ disableAppNotification(pkgName);
}
}
}
+ private void disableAppNotification(String packagename){
+ //NotificationManager mNotificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+ INotificationManager mNotificationManager = INotificationManager.Stub.asInterface(
+ ServiceManager.getService(Context.NOTIFICATION_SERVICE));
+ try {
+ int uid = getPackageUid(packagename, 0, UserHandle.USER_SYSTEM);
+ Slog.i("Tim_A","PMS getPackageUid disable app pkg="+ packagename+",uid="+uid);
+ mNotificationManager.setNotificationsEnabledForPackage(packagename, uid, false);
+ } catch (Exception e) {
+ Slog.e("Tim_A", "PMS Error calling NoMan" + e);
+ }
+ }
+
private void startIntentFilterVerifications(int userId, boolean replacing,
PackageParser.Package pkg) {
if (mIntentFilterVerifierComponent == null) {
在PMS里面当应用安装好后就关闭掉此应用的通知。