I am trying to make an app in which i am using broadcast receiver. My requirement is to keep my broadcast receiver alive when i install my application in the phone and exiting out after running it. I am able to get it up to this requirement,but when I am killing the application with the use of Task manager, broadcast receiver is not triggering, and I also want to keep my broadcast receiver alive even somebody restarting the phone.here is my code for 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 is my broadcast receiver code:
@Override
public void on Receive(Context context, Intent intent) {
// TO DO 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 is my 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 help me!!!..... Thank you in advance.
you can create a service and register the broadcast receiver in that service. You should start the service using startService so that it runs always. For running it after reboot create a broadcast receiver which will registered for booting complete action and this will start the ^service.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.