Receive intent “android.settings.APPLICATION_DETAI

2019-03-31 13:54发布

问题:

I want to get the package name and class name of the received intent, But am not able to get it. I want to make my app secure so it asks for password before being uninstalled. Only the user who Installed the app knows the password, so only he/she can uninstall the app.

My code for Receiver:

public class PackageReceiver extends BroadcastReceiver { 
@ Override 
public void onReceive (Context context, Intent intent) { 
if (intent.getAction().equals("android.settings.APPLICATION_DETAILS_SETTINGS")) { 
/ / TODO: 
//I want here to get this getAction working and then I want to fetch package and class of the intent
} 

} 
} 

Manifest:

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.RESTART_PACKAGES"/> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

<Application 
android: icon = "@ drawable / ic_launcher" 
android: label = "Test"> 

<Receiver android: name = ". PackageReceiver" 
android: label = "@ string / app_name"> 
<intent-filter> 
<action android:name="android.settings.APPLICATION_DETAILS_SETTINGS" /> 
<data android:scheme="package" /> 
</ Intent-filter> 
</ Receiver> 
</ Application> 

Please let me know if I am missing any permission because I can not get this working.

回答1:

I personally think this behavior is annoying. And for sure it's redundant: there are other mechanisms already on place that tackle the same problem (screen locking, encryption).

I'd only use extra checks when operations are on your side (ie.: delete account, change email, etc).

If I didn't make it to discourage you to do that here's another post that solves the same problem following a similar direction.



回答2:

When we select a particular app inside the settings screen, a broadcast of the type : android.intent.action.QUERY_PACKAGE_RESTART is fired with package name of the application as an extra. I think you could use that to fire the password dialog.

The receiver code will be something like this :

public void onReceive(Context context, Intent intent) {
    String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES"); 

    if(packageNames!=null){
        for(String packageName: packageNames){
            if(packageName!=null && packageName.equals("our_app_package_name")){
                //Our app was selected inside settings. Fire Password Dialog. 
            }
        }
    }
}


回答3:

I think intent.getExtra("com.android.settings.ApplicationPkgName") should have the package name



回答4:

i dont know if its acceptable

setting package by getting packagename from the context of activity

intent.setPackage(context.getPackageName());

and to get package name

intent.getPackage()