I am doing an application in which I want to call a number when that device boot completed. My code is like this:
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("**inside onRecevier");
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.test.app.TestService");
context.startService(serviceIntent);
}
First I created BroadcastReceiver. I registered this receiver in manifest file like this:
<receiver android:name="TestReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
In a receiver I called the below service:
public class TestService extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("**inside onCreate");
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555"));
startActivity(call);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("**inside onDestroy");
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
System.out.println("**inside onStart");
super.onStart(intent, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
}
when I tried to boot a device after booting the application getting force close. How to do this in android??
Thanx in advance
You need to add the NEW_TASK flag before starting an activity from a service:
This explains:
Also, you must hold the permissions:
RECEIVE_BOOT_COMPLETEDCALL_PHONEAnd as Waqas mentioned, it would be better for you to start your service from your
onReceivelike:Make sure you’ve done all that I’ve stated, and if you continue to have an issue, then it would be helpful if you edit your question and paste the logcat from the force close.