0

I need some help regarding getting the package name or the application name that is using a specific intent say Camera.

I am developing an application that scans which application is using the camera in background or some other like microphone etc. But I couldn't find any way to get the such detail. Any help would be appreciated. Thanks

Akshay Sood
  • 6,366
  • 10
  • 36
  • 59
  • Also please tell me if I can display a dialog asking some permission for using the camera like in marshmallow 6.0. I need such thing to do in device below marshmallow 23 api. Like launching the camera app should ask the permission in a dialog and if I click on no it should close the camera app. – Akshay Sood Mar 15 '16 at 21:19
  • "that is using a specific intent say Camera" -- "Camera" is not an `Intent`. "But I couldn't find any way to get the such detail" -- hopefully, that is not available to you, except perhaps on rooted devices, for privacy reasons. – CommonsWare Mar 15 '16 at 21:19
  • @CommonsWare yeah intent for using camera.. can I do this using device administrator . Like enabling the app as device admin? – Akshay Sood Mar 15 '16 at 21:22
  • I am not aware that the device admin APIs allow you to spy on what the user is doing with their device, as you seek here. That being said, those APIs have changed a lot in the past couple of years, though most of those changes are tied to 'device owners', which have to be set up when the device is first turned on after purchase. – CommonsWare Mar 15 '16 at 21:32

1 Answers1

1

Also please tell me if I can display a dialog asking some permission for using the camera like in marshmallow 6.0. I need such thing to do in device below marshmallow 23 api. Like launching the camera app should ask the permission in a dialog and if I click on no it should close the camera app

For Android Marshmallow , whenever you want to launch camera you should check whether you have permissions for accessing camera and if not you can request for permissions by showing the system dialog -

 if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 1001); //May be any number
  } else {
       //Launch camera since you have the permission
}

Also, you have to implement onRequestPermissionsResult() callback so that you can check the user action on the permission dialog and react accordingly -

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1001 : //Same request code 
                if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(MainActivity.this,"Camera permission requied", Toast.LENGTH_LONG).show();

                } else if (grantResults.length > 0 && (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
                    //Launch camera
                }
                break;
        }
    }
Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
  • I am not asking for 6.0 sir. I need such thing for android 5.0 , 4.0 ,... not for marshmallow . I know in marshmallow its possible. But I need this thing in other versions also – Akshay Sood Mar 15 '16 at 21:40
  • Oh ! But why do you require that since before 6.0, the permissions are already granted if you specify them in your Manifest file ? – Shadab Ansari Mar 15 '16 at 21:45
  • what if I need the same functionality in 5.0? or 4.0 – Akshay Sood Mar 18 '16 at 15:50
  • @AkshaySood You can't have the same functionality on android 5.1 or lower, check first paragraph: https://developer.android.com/training/permissions/requesting.html "If the device is running Android 5.1 or lower, or your app's target SDK is 22 or lower: If you list a dangerous permission in your manifest, the user has to grant the permission when they install the app; if they do not grant the permission, the system does not install the app at all. " – AppiDevo Mar 20 '17 at 13:42