Service don't start after boot - android

I have a service in my Android app that works perfectly if i started manually, but i need that service to start on boot, and i can't get it started automatically after rebooting the phone, i tryed several tutorials but nothing works.
Here is my manifest code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tvshowsguide"
android:versionCode="1"
android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
<service android:name="androidservice.SyncService" />
<receiver android:name="androidservice.SyncAuto" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is the BroadcastReceiver code:
public class SyncAuto extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent SyncService = new Intent(context, SyncService.class).setAction(SRVC);
context.startService(SyncService);
}
}
}

I think the problem must be the reference of your service, since you have an application with
package="com.tvshowsguide"
Your service and receiver must have a name like:
<service android:name="com.tvshowsguide.androidservice.SyncService" />
<receiver android:name="com.tvshowsguide.androidservice.SyncAuto" >

Related

Xamarin, Android and BroadcastReceiver

In my app I want to create a service to save constantly a device location. This service has to start when the phone is rebooted.
I changed the Android.Manifest like this
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="LocationTest">
<receiver>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
I also changed the received tag like this
<receiver android:permission="RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I defined a BroadcastReceiver like
[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
public class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
Intent i = new Intent(context, typeof(LocationService));
i.AddFlags(ActivityFlags.NewTask);
context.StartService(i);
}
}
I created another one from Xamarin Template like
[BroadcastReceiver]
public class BroadcastReceiverTest : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "New Received intent!", ToastLength.Short).Show();
}
}
If I reboot my device, no one of the BroadcastReceiver is fired. I don't know how I can do that. My project is on GitHub
Thank you in advance.
Although it is possible to find a lot of posts about this question on line, I couldn't find one with all right information. I have created a post for future reference here.
The important thing is in your AndroidManifest: you mustn't declare your broadcast receiver in it. If you have any services, you must declare them in it. For example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1" android:versionName="1.0"
package="pro.wordbank.app.locationtest">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="LocationTest">
<service android:name=".TimerService" />
<service android:name=".LocationService" />
</application>
</manifest>
Also, it isn't possible to call Toast.MakeText from your Broadcast Receiver but you have to use instead NotificationManager.

android; service autostarting after boot: FLAG_INCLUDE_STOPPED_PACKAGES not working

I have read several answers about this question, but the posted solution doesn't work for me. Probably there is something wrong or missed in my code.
I need that my app, with no activity, starts automatically after the boot completed.
If I include an activty, just to start for the first time the app (exiting the stopped state), everything works.
Thank you in advance for your help.
Here is my code.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zag.salva" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".Salva_autostart"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".Salva_servizio"
android:enabled="true" >
<intent-filter>
<action android:name=".Salva_servizio" />
</intent-filter>
</service>
</application>
Salva_autostart.java
public class Salva_autostart extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Intent intento = new Intent(context, Salva_servizio.class);
intento.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.startService(intento);
}
}
Salva_servizio.java
public class Salva_servizio extends Service
{
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// Task execution
Salva_invio2 invio = new Salva_invio2();
invio.esegui(this);
return Service.START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
}
You shouldn’t add FLAG_INCLUDE_STOPPED_PACKAGES to your receiver's intent that start your service. You have to add it to the intent that you use for sendBroadcast. Meaning, you need to add it to the intent in the application that invokes the Broadcast.
That is why this flag is irrelevant in your code.
If you will sendBroadcast to this receiver ("Salva_autostart") just once, from outside your application - then your application will not be in "force stop" state any more and on next boot your receiver will be triggered.
Also you should add (addFlags) and not set (setFlags).
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
This is how you should trigger your receiver from another application:
Intent intent = new Intent("com.xxx.my_filter_intent");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
this.sendBroadcast(intent);
On your manifest add the above filter intent to your receiver (you can add it in new <intent-filter> or in the same that you already have for the BOOT_COMPLETED action.
<receiver
android:name=".Salva_autostart"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="com.xxx.my_filter_intent" />
</intent-filter>
</receiver>
Read more here:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
Note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.

Unable to start Service after Device Boot Completed. Boot Completed BroadcastReceiver onReceive() not Calling

I am working on one app which will start a service automatically at device start ( after boot complete ). i did code but i think something is going wrong with my code. i have tried google too and all the tricks while research. can you please help me? Thanx in advance.
here is my code :
My BroadcastReceiver is
public class AutostartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Toast.makeText(this, "Booting Completed", Toast.LENGTH_LONG).show();
//context.startService(new Intent(context, MyService.class));
}
}
}
My AndroidMenifest file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myhiddenapp"
android:installLocation="internalOnly"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="MyHiddenApp" >
<receiver
android:name=".AutostartReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyService" />
</application>
</manifest>
Note : I have not any Activity, i just want to start Service on Boot Complete.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
... >
You Must set android:installLocation="internalOnly"
https://developer.android.com/guide/topics/data/install-location.html#Should

How to implement one BroadcasReceiver and another acitivity both as a Launcher activity

I have two launcher activities,
1. Receiver activity which is a Broadcast Receiver.
2. An Activity which should be main Launcher activity.
When an SMS arrives the receiver activity launches the .MainActivity(BroadcastReceiver) and it further start one service. (Without GUI) and without opening Settings activity.
Another activity .Settings is Main launcher activity.
Goal I want to Achive:
When sms arrived receiver activity work as it is. But When I want to change the settings Setting activity starts.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.aa.FindLocation"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.aa.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.aa.Settings"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.aa.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MainActivity"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<service android:name=".Servc" >
<intent-filter>
<action android:name="com.example.Servc" />
</intent-filter>
</service>
</application>
</manifest>
Problem
1.
[2014-07-06 02:55:14 - Aa] Uploading Aa.apk onto device 'S5830f4524b76'
[2014-07-06 02:55:14 - Aa] Installing Aa.apk...
[2014-07-06 02:55:18 - Aa] Success!
[2014-07-06 02:55:18 - Aa] Starting activity com.example.aa.Settings on device S5830f4524b76
[2014-07-06 02:55:19 - Aa] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.aa/.Settings }
2.
What changes should be made in manifest to make it working?
Don't define your sms receiver in the manifest. Remove it.
Create Broadcast receiver that runs on boot up by defining the below element in your manifest :
<receiver android:name=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
You will need to add this permission to your manifest :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Then register your sms receiving BroadcastReceiver in there :
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
SmsReceiver smsreceiver = new SmsReceiver();
IntentFilter fltr_smsreceived = new IntentFilter(
"android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsreceiver, fltr_smsreceived);
}
}
SmsReceiver is your broadcast receiver. For example :
public class SmsReceiver extends BroadcastReceiver {
void setActivityHandler(){
//instantiate member variables if required
}
#Override
public void onReceive(Context context, Intent intent) {
Bundle pdusBundle = intent.getExtras();
Object[] pdus=(Object[])pdusBundle.get("pdus");
SmsMessage messages=SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.d("tag","message body : " + messages.getMessageBody());
}
}

Manifest / Activity error

I am trying to get my app to auto start on boot up and it will and an error occurs while launching the application
Here is my manifest and the program file for the "Auto Starter":
manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="this.bad.file"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="autoBot"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is the "AutoBot" as I have called it (Not for spammy reasons I just like transformers):
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class autoBot extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent startUp = new Intent(context, MainActivity.class);
context.startActivity(startUp);
}
}
So there we have it!
In your manifest, you left out a vital name of the class, usually, can be abbreviated to [dot][ClassName] or the full package name, as in for example .autoBot or see the example below:
<receiver android:name="this.bad.file.autoBot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And in your Broadcast receiver:
public class autoBot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startUp = new Intent(context, MainActivity.class);
context.startActivity(startUp);
}
}
Notice the usage of #Override on the onReceive class.
The normal recommended route to take is usually, to start an alarm on boot, and listen for the broadcast when the alarm has expired, in that way your activity does not hog up the boot and also, allow the boot up process to "settle" for a bit.

Categories

Resources