Q:

lockNow() in broadcast receiver

it seems that i get an error when i try to use LockNow() in the broadcast receiver. can anyone help me.
public class Onlockreceive extends BroadcastReceiver {

LockSettings lockactivity;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
lockactivity.mdevicepolicymanager.lockNow();    

} 
}

The error message :

12-31 03:14:16.322: D/AndroidRuntime(27478): Shutting down VM 12-31    03:14:16.322: W/dalvikvm(27478): threadid=1: thread exiting with    uncaught exception (group=0x41b0d378) 12-31 03:14:16.332:    E/AndroidRuntime(27478): FATAL EXCEPTION: main 12-31 03:14:16.332:    E/AndroidRuntime(27478): java.lang.RuntimeException: Unable to start    receiver com.example.settings2.Onlockreceive:    java.lang.NullPointerException 12-31 03:14:16.332:    E/AndroidRuntime(27478): at    android.app.ActivityThread.handleReceiver(ActivityThread.java:2257)    12-31 03:14:16.332: E/AndroidRuntime(27478): at    android.app.ActivityThread.access$1500(ActivityThread.java:138) 12-31    03:14:16.332: E/AndroidRuntime(27478): at    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)    12-31 03:14:16.332: E/AndroidRuntime(27478): at    android.os.Handler.dispatchMessage(Handler.java:99) 12-31    03:14:16.332: E/AndroidRuntime(27478): at    android.os.Looper.loop(Looper.java:213) 12-31 03:14:16.332:    E/AndroidRuntime(27478): at    android.app.ActivityThread.main(ActivityThread.java:4787) 12-31    03:14:16.332: E/AndroidRuntime(27478): at    java.lang.reflect.Method.invokeNative(Native Method) 12-31    03:14:16.332: E/AndroidRuntime(27478): at    java.lang.reflect.Method.invoke(Method.java:511) 12-31 03:14:16.332:    E/AndroidRuntime(27478): at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)    12-31 03:14:16.332: E/AndroidRuntime(27478): at    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 12-31    03:14:16.332: E/AndroidRuntime(27478): at    dalvik.system.NativeStart.main(Native Method) 12-31 03:14:16.332:    E/AndroidRuntime(27478): Caused by: java.lang.NullPointerException    12-31 03:14:16.332: E/AndroidRuntime(27478): at    com.example.settings2.Onlockreceive.onReceive(Onlockreceive.java:15)    12-31 03:14:16.332: E/AndroidRuntime(27478): at    android.app.ActivityThread.handleReceiver(ActivityThread.java:2250)    12-31 03:14:16.332: E/AndroidRuntime(27478): ... 10 more

My problem is similar to this one same problem.. but i don't understand how to fix it 
this is the code i use to call the broadcast receiver
private void setupAlarm(int seconds,boolean s) {

// Finish the currently running activity
   // MainActivity.this.finish();

  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  Intent intent = new Intent(getBaseContext(), Onlockreceive.class);

  PendingIntent pendingIntent = PendingIntent.getBroadcast(
     LockSettings.this, 0, intent,
     PendingIntent.FLAG_UPDATE_CURRENT);

  Log.d(TAG, "Setup the alarm");

  // Getting current time and add the seconds in it
  Calendar cal = Calendar.getInstance();
  cal.add(Calendar.SECOND, seconds);

if (s == true){
  //alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
  //alarmManager.setInexactRepeating(type, triggerAtMillis, intervalMillis, operation)
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, pendingIntent   );
}else if (s == false)
{
    alarmManager.cancel(pendingIntent);
}

}

A:

Either lockactivity or mdevicepolicymanager is null. This is not surprising, as your process may well have been terminated before the BroadcastReceiver got control.
Static data members are only a cache. Do not rely upon them. And most certainly do not put an Activity in a static data member, as you are leaking a lot of memory by doing so.
You can use the Context passed into onReceive() to retrieve a DevicePolicyManager and call lockNow().
UPDATE
Here is the revised class, complete with better formatting and case:
public class OnLockReceive extends BroadcastReceiver {  
  @Override
  public void onReceive(Context context, Intent intent) {
    ((DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE)).lockNow();
  } 
}

