0

I can call my function on the click of a button with no issues like this and it works, also wipes data.

public class MainActivity extends Activity implements OnCheckedChangeListener {
DevicePolicyManager devicePolicyManager;


 public void resetdevice(View view) { 
Toast.makeText(context, "resetttt", Toast.LENGTH_LONG).show();
    // We reset the device - this will erase entire /data partition!
    Log.d(TAG,
            "RESETing device now - all user data will be ERASED to factory settings");
    devicePolicyManager.wipeData(ACTIVATION_REQUEST);
}

but, in order to call this method from another service, i need to do some changes.

public class MainActivity extends Activity implements OnCheckedChangeListener {
static  DevicePolicyManager devicePolicyManager;

and my method

public static void resetdevice(Context context) {
    Toast.makeText(context, "resetttt", Toast.LENGTH_LONG).show();
    // We reset the device - this will erase entire /data partition!
    Log.d(TAG,
            "RESETing device now - all user data will be ERASED to factory settings");
    devicePolicyManager.wipeData(ACTIVATION_REQUEST);
}

here is how i call it

MainActivity.resetdevice(context);

the problem is, when calling it from my service, it only shows toast, and doesn't execute rest of the code.

abbie
  • 345
  • 5
  • 22

4 Answers4

0

If you are good enough to use event bus than implement event bus in your code and when you want to call method just fire event from your service will call your method where ever you have implemented.

or

Try to use broadcast receiver and send broad cast when you want to call your method from service.

J.D.
  • 1,401
  • 1
  • 12
  • 21
0

You cannot directly call activity method from service like that. Please use either broadcast or bind object. checkout one ex here

Community
  • 1
  • 1
Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25
0

Try this:

    public static void resetdevice(Context context) {
    DevicePolicyManager devicePolicyManager= (DevicePolicyManager) context
 .getSystemService(Context.DEVICE_POLICY_SERVICE);
        Toast.makeText(context, "resetttt", Toast.LENGTH_LONG).show();
        // We reset the device - this will erase entire /data partition!
        Log.d(TAG,
                "RESETing device now - all user data will be ERASED to factory settings");
        devicePolicyManager.wipeData(ACTIVATION_REQUEST);
    }

However I do recommend doing it the way as said by @jack.

rusted brain
  • 1,052
  • 10
  • 23
0

You may want to check Bound Services. You can use this, to notify your Activity that it needs to call resetDevice from your Activity using Messenger and Handlers.

ekouChiq
  • 244
  • 2
  • 10