Start service when device boot completed - android

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:
Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555"));
call.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(call);
This explains:
Note that if this method is being called from outside of an Activity
Context, then the Intent must include the FLAG_ACTIVITY_NEW_TASK
launch flag. This is because, without being started from an existing
Activity, there is no existing task in which to place the new activity
and thus it needs to be placed in its own separate task.
Also, you must hold the permissions:
RECEIVE_BOOT_COMPLETED
CALL_PHONE
And as Waqas mentioned, it would be better for you to start your service from your onReceive like:
Intent serviceIntent = new Intent(context, TestService.class);
context.startService(serviceIntent);
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.

change your onReceive to this
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("**inside onRecevier");
Intent serviceIntent = new Intent(context, TestService.class);
context.startService(serviceIntent);
}

There's a variety of reasons why you'd get a Force Close. The best way to tell which is to look at the log. It should tell you exactly what exception is being thrown, and give you a hint as to how to solve the problem.

Related

Starting service from another service

I'm trying to start a service from another service. But wonder what's going wrong. The code is like
class Service1 extends GCMBaseIntentService {
#Override
protected void onMessage(Context arg0, Intent intent) {
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
Intent service = new Intent(getApplicationContext(), Service2.class);
startService(service);
}
}
And Service2 is
class Service2 extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_LONG).show();
}
}
I'm getting the Toast "Hello" in Service1 but not getting Toast "Service Started" from Service2
Instead of using getApplicationContext() you should use Service1.this or getBaseContext() . Have you declared your Service2 in the AndroidManifest?

android alarm doesn't work if i press back button

I have implemented an Alarm manager and receiver in my application, all is working perfectly. The issue I am having is when I press the back button to close the application, the alarm doesn't run at the time specified. Below is the code I am using:
My Receiver Code
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("SERVICE RECIEVED");
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
My Alarm Service Code
public class MyAlarmService extends Service {
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
#SuppressWarnings("static-access")
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Intent _intent = new Intent(getBaseContext(), FirstCallActivity.class);
_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(_intent);
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
Start Alarm Code
public void startAlarm(Calendar cal) {
// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(this, FirstCallActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 12345,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
Manifest Code (within application tags)
<service
android:name=".MyAlarmService"
android:enabled="true" />
<receiver android:name=".MyReceiver" />
Could anyone explain why this is happening? I know if the application is fully closed nothing is going to happen, but I it seems strange the back button causing this issue because the application is still running in the background.
Thanks
It turns out that the app was being closed on the back button. By stopping the app closing on the back button press with the code below, the function required for the set alarm run correctly.
#Override
public void onBackPressed() {
System.out.println("BACK PRESS");
moveTaskToBack(true);
}

How can I check that whether a broadcast receiver is working or not?

Here is bootstartup.java file. I have checked that TestService is working properly. But I just want to know how can I check that data is broadcasted?
Code
public class bootstartup extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// start the service
Intent service=new Intent(context,TestService.class);
context.startService(service);
Log.e("com.example.myglobalbroadcastreceiver","Broadcast Received:"+intent.getAction());
// start the app
Intent app= new Intent(context,Abhijeet.class);
context.startService(app);
}
}

Android Service's method onDestroy called before Bluetooth scanning ends

I have a problem that I start a service with an Intent, in onCreate() method register BroadcastReceiver and then in onDestroy() unregister the BroadcastReceiver. In Method onHandleIntent() I start discovery for bluetooth devices. THe problem is, that I my BroadcastReceiver is destroyed before any device is found so I cant really do anything with the BluetoothDevice.ACTION_FOUND intent. I tried to move startDiscovery() to onCreate() but no results. My code looks like this:
public class BluetoothService extends IntentService{
private BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
private final String SMART_TOKEN_ADDRESS = "F0:E7:7E:5F:63:70";
private final BroadcastReceiver receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v("BLUETOOTH","found it!");
}
};
public BluetoothService() {
super("BluetoothService");
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(){
super.onCreate();
Log.v("SERVICE", "Just got created");
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
btAdapter.startDiscovery();
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Log.v("BLUETOOTH", "Discovery just started");
//btAdapter.startDiscovery();
}
#Override
public void onDestroy() {
super.onDestroy();
Log.v("BLUETOOTH","Just got destroyed!");
unregisterReceiver(receiver);
}
}
Log looks like this:
1. Just got created
2. Discovery just started
3. Just got destroyed!
4. Received android.bluetooth.device.action.FOUND
Thanks for help!
T.
This is happening because after handling all the requests, the service is stoped. From documentation: "Stops the service after all start requests have been handled, so you never have to call stopSelf()."
So, you should use wait or other command to tell the worker thread to wait the execution, if you want to use service.
Please, refer to the documentation about services: http://developer.android.com/guide/components/services.html

Calling an Activity from a Service

I have a service class for an alarm service containing the service methods. These methods are called when the alarm service is activated. What I want to do is to call an intent to another class in one of these methods that are called in the service class (when the alarm goes off). All it does is just flag up errors when calling the intent. This only happens in the methods that are called when the alarm service is activated (methods in service class). Is this because the class extends Service and not extends Activity? I'm not sure, any ides?
(Below is my service class, when the intent to another activity is called in the onStart method the app force closes.)
public class MyAlarmService extends Service {
#Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
//////////////////////////////////////////////////////////////////////////////////
Intent i = new Intent("com.exercise.AndroidAlarmService.HELLO");
startActivity(i);
The intent that is send to open another class, an activity.
**
/////////////////////////////////////////////////////////////////////////////////
}
#Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
}
One of these errors on the LogCat is:
06-24 01:11:36.857: E/AndroidRuntime(10805): java.lang.RuntimeException: Unable to start service com.exercise.AndroidAlarmService.MyAlarmService#412f23f8 with Intent { flg=0x4 cmp=com.exercise.AndroidAlarmService/.MyAlarmService (has extras) }: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Have you tried what the error log suggests?
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
You can call an Activity using onStart() of your service.....
#Override
public void onStart(Intent intent, int startId) {
...
Log.i("Service", "onStart() is called");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setClass(<Set your package name and class name here>);
startActivity(callIntent);
...
}
You can do it by enabling the flag as suggested by others. The reason why it is prevented by default is because services are prone to automatic restart by system, in the background. If you are starting an activity during onStart of a service, this activity starts irrespective of what the user may be doing. This will be bad user experience. Please bear this caveat in mind and have work around for this scenario.

Categories

Resources