android - How to keep broadcast recevier alive even after restarting the phone? -
i trying make app in using broadcast receiver. requirement keep broadcast receiver alive when install application in phone , exiting out after running it. able requirement,but when killing application use of task manager, broadcast receiver not triggering, , want keep broadcast receiver alive restarting phone.here code main activity:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); telephonymanager = (telephonymanager) getsystemservice(context.telephony_service); phonestatelistener psl = new phonestatelistener() { @override public void oncallstatechanged(int state, string incomingnumber) { // todo auto-generated method stub if (state == telephonymanager.call_state_ringing) { intent intent2open = new intent(mainactivity.this, mainactivity.class); intent2open.setflags(intent.flag_activity_brought_to_front | intent.flag_activity_new_task | intent.flag_activity_previous_is_top | intent.flag_activity_multiple_task); intent2open.setaction("android.intent.action.view"); intent2open.addflags(intent.flag_activity_clear_top); intent2open.setaction(intent.action_main); intent2open.addcategory(intent.category_launcher); intent2open.setflags(0); toast.make_text(context(), "incoming call" + incomingnumber, toast.lengthlong) .show(); } super.oncallstatechanged(state, incomingnumber); } }; telephony manager.listen(psl, phonestatelistener.listen_call_state); }
below broadcast receiver code:
@override public void on receive(context context, intent intent) { // auto-generated method stub string state = intent.get string extra(telephony manager.action_phone_state_changed); if(state.equals(telephony manager.call_state_ringing)) { intent intent2 = new intent("com.example.app.activity"); intent2.putextras(intent2); intent2.addflags(intent.flag_activity_new_task); context.startactivity(intent2); } }
below manifest.xml code:
<application <activity android:name="com.example.app.activity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name="com.example.app.receiver" android:exported="true" android:enabled="true"> <intent-filter> <action android:name="com.example.app.activity" /> </intent-filter> </receiver> </application>
please me!!!..... thank in advance.
you can create service , register broadcast receiver in service. should start service using startservice runs always. running after reboot create broadcast receiver registered booting complete action , start ^service.
Comments
Post a Comment