2015-12-31 9 views
5

मैं इस कोड का उपयोग यह पता लगाने के लिए कर रहा हूं कि स्क्रीन लॉक हो जाने पर और टोस्ट को कॉल करने के लिए, यह हर बार स्क्रीन लॉक होने पर काम करता है। हालांकि, जब भी मैं ऐप से बाहर जाता हूं, यह काम करना बंद कर देता है। यह केवल तभी काम करता है जब ऐप खुला हो।ऑन रिसीव केवल एक बार एंड्रॉइड

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

+0

ऐसा इसलिए है क्योंकि unregisterReceive अपने ब्रॉडकास्टसीवर को रोकें, ऑनस्टॉप या ऑनस्टरी विधि पर? – xxxzhi

उत्तर

0

उपयोग BroadcastReceiver WakefulBroadcastReceiver बजाय

इस कोड को मिला दर्ज की

http://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

+1

बस सोच रहा है, प्रसारण रीसीवर पृष्ठभूमि में क्यों काम करता है जब इसे – MaggotSauceYumYum

+0

जैसा डिज़ाइन किया गया है, आपको ब्रॉडकास्ट रिसीवर के लिए अनुमति और wakelock जागने की आवश्यकता है। WakefullBroadCastReceiver स्वयं कर रहा है .. :) –

1

मुझे लगता है कि आप task() विधि में अपनी प्रक्रिया सहित सभी प्रक्रियाओं को मार रहे हैं। killBackgroundProcesses() से अपनी पृष्ठभूमि सेवा की प्रक्रिया को फ़िल्टर करें।

RunningAppProcessInfo से प्रक्रिया का नाम प्राप्त करें और इसे अपने ऐप्स के प्रक्रिया नाम से तुलना करें। डिफ़ॉल्ट रूप से, प्रक्रिया का नाम पैकेज नाम के बराबर होगा।

private void task(Context context) { 
    ActivityManager actvityManager = (ActivityManager) context 
      .getApplicationContext().getSystemService(
        context.getApplicationContext().ACTIVITY_SERVICE); 
    List<ActivityManager.RunningAppProcessInfo> procInfos = actvityManager 
      .getRunningAppProcesses(); 
    for (int pnum = 0; pnum < procInfos.size(); pnum++) { 
     ActivityManager.RunningAppProcessInfo info = procInfos.get(pnum); 

     if (info.pid != android.os.Process.myPid()) { 
      actvityManager.killBackgroundProcesses(info.processName); 
     } 
    } 
} 
+0

संपादित उत्तर पढ़ें – Msp

+0

अपने पैकेज का नाम फ़िल्टर करना बेकार नहीं है। यह अपनी प्रक्रिया को मार सकता है। जवाब पढ़ें [यहां] (http://stackoverflow.com/a/25508685/1542531)। – Msp

+0

सुनिश्चित करें कि आपकी प्रक्रिया का नाम और पैकेज का नाम समान है। – Msp

-1

मैं एक अधिक कुशल और विश्वसनीय समाधान नहीं मिला, मैं एक notificationmanager जो हर 5 सेकंड काम करता है और अपने मुख्य गतिविधि में विधि कॉल पंजीकृत!

MainActivity.java

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // ENABLE MyReceiver SERVICE 
     ComponentName receiver = new ComponentName(MainActivity.this, 
       NotifyService.class); 
     PackageManager pm = this.getPackageManager(); 
     pm.setComponentEnabledSetting(receiver, 
       PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
       PackageManager.DONT_KILL_APP); 
     // Toast.makeText(this, "Enabled broadcast receiver", 
     // Toast.LENGTH_SHORT) 
     // .show(); 
     // --// 

     Intent intent = new Intent(this, NotifyService.class); 
     PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 
       PendingIntent.FLAG_CANCEL_CURRENT); 

     AlarmManager am = (AlarmManager) this 
       .getSystemService(Context.ALARM_SERVICE); 
     long recurring = (1 * 1000 * 5); // in milliseconds 
     am.setRepeating(AlarmManager.RTC, Calendar.getInstance() 
       .getTimeInMillis(), recurring, sender); 
    } 

    public static void mehtodName(Context context) { 
     KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); 
      if(myKM.inKeyguardRestrictedInputMode()) { 
       // it is locked 
       task(context); 
      } else { 
       //it is not locked 
      } 
    } 

    private static 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); 
      } 
     } 
    } 

NotifyService.java

public class NotifyService extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     MainActivity.mehtodName(context); 
     // ... do what you need to do here... 
    } 

} 

मेरे प्रकट

<receiver android:name="com.example.notifypro.NotifyService" > 
</receiver> 
संबंधित मुद्दे