मैं इस कोड का उपयोग यह पता लगाने के लिए कर रहा हूं कि स्क्रीन लॉक हो जाने पर और टोस्ट को कॉल करने के लिए, यह हर बार स्क्रीन लॉक होने पर काम करता है। हालांकि, जब भी मैं ऐप से बाहर जाता हूं, यह काम करना बंद कर देता है। यह केवल तभी काम करता है जब ऐप खुला हो।ऑन रिसीव केवल एक बार एंड्रॉइड
public class BatterySaverLiteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.i("Check", "Screen went OFF");
Toast.makeText(context, "screen OFF", Toast.LENGTH_LONG).show();
task(context);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.i("Check", "Screen went ON");
Toast.makeText(context, "screen ON", Toast.LENGTH_LONG).show();
}
}
private void task(Context context) {
// Process Killer and display all package names in toast
ActivityManager actvityManager = (ActivityManager) context
.getApplicationContext().getSystemService(
context.getApplicationContext().ACTIVITY_SERVICE);
List<RunningAppProcessInfo> procInfos = actvityManager
.getRunningAppProcesses();
for (int pnum = 0; pnum < procInfos.size(); pnum++) {
actvityManager
.killBackgroundProcesses(procInfos.get(pnum).processName);
}
}
}
thats कैसे im मेरी रिसीवर
प्रकट
<receiver android:name=".BatterySaverUltraReceiver" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
मैं से here
ऐसा इसलिए है क्योंकि unregisterReceive अपने ब्रॉडकास्टसीवर को रोकें, ऑनस्टॉप या ऑनस्टरी विधि पर? – xxxzhi