Hi i have developed an service application using activity and that works great in 2.2 and 2.3 android version and it starts on boot and runs my app for once in 30 mins and sending location to server but in 4.0 the app is not running on services on boot can anybody say me why?
my code:
BroadcastReceiver.java:
public class autostart extends BroadcastReceiver {
public void onReceive(Context arg0, Intent arg1) {
if ("android.intent.action.BOOT_COMPLETED".equals(arg1.getAction())) {
Intent intent = new Intent(arg0, gps_back_process.class);
arg0.startService(intent);
Log.i("Autostart", "started");
}
}
}
service.java:
public class gps_back_process extends Service
{
private static final String TAG = "MyService";
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("Testing", "Service got created");
Toast.makeText(this, "gps_back_process,onCreate();", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startid)
{
Intent intents = new Intent(getBaseContext(),MainActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "gps_back_process.onCreate();", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<receiver android:name=".autostart" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity" >
</activity>
<service
android:name=".gps_back_process"
android:enabled="true" />
</application>
thank you.
Once the user runs the app for the first time (and does not force stop it), everything behaves as before — a reboot will cause BOOT_COMPLETED broadcasts to be received and so on. However, if the user installs the app, until and unless they run the app manually, no broadcasts will be received. And if the user force-stops the app, until and unless they run the app manually, no broadcasts will be received.
Check this for more detail.
This happens because from Android 3.1+ you Service will not run on Boot unless you start(launch) your Application atleast once after installation. So, when you install your Application and restart the device prior of launching the Applications MainActivity your BroadCastReceiver won't be fired. For that you have to launch your MainActivity once and then restart the device. That works!
For reference you can check my question here.
You should add add android.permission.RECEIVE_BOOT_COMPLETED,
If you don't have this permission your app won't receive the boot completed intent.
Related
I trying to write simple basic application that will start my service when the phone is start.
i add all the permission i need.
I install the application on my android ( android 6.01 ).
And when i reboot my phone - i can't see that the service is up or did any action.
why this is not working ?
how can i debug a service on android ?
The code:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAG" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<service android:name=".MyService" android:enabled="true" android:exported="true"/>
<receiver android:name=".MainBroadcastReceiver" android:enabled="true" android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
public class MainBroadcastReceiver extends BroadcastReceiver {
public MainBroadcastReceiver(){
}
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MyService.class));
}
}
public class MyService extends Service {
private File _file;
private Timer _timer;
private FileOutputStream _fileOutputStream;
public MyService() throws IOException {
_file = new File("/sdcard/" + "myServiceText.txt");
_file.createNewFile();
_fileOutputStream = openFileOutput(_file.getName(), Context.MODE_APPEND);
_fileOutputStream.write("Ctor called".getBytes());
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
_timer = new Timer("timer");
_timer.schedule(new TimerTask()
{
#Override
public void run()
{
try {
_fileOutputStream.write("onCreate Called".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}, 0, 5000);
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
_fileOutputStream.write("onStartCommand".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return START_STICKY;
}
Your code is fine, the reason your service is not running it's because your app is in Stopped State.
1. Stopped state is a security procedure that allows your app to "wake itself up" only after user first manually launches it from the home screen.(Unless you are a system)
By doing so android prevents malicious apps to be installed and run in your phone background without you knowing.
2. How to debug a background process?
When working with background processes there are two great tools you need in your belt -
First is the adb broadcast command -
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
Second is attach remote debugger (Android Studio):
Run | Attach debugger to android process (last option in the menu) | choose your app's process.
I want to make two android services that kip running on background even when my app is closed or when all system reboots, but I have no idea about android. And that's what I did:
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_ROOT_COMPLETED"/>
<service android:name="FirstService"></service>
<receiver android:name="FirstServiceReceiver">
<intent-filter>
<action android:name="com.android.techrainner"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="SecondService"></service>
<receiver android:name="SecondServiceReceiver">
<intent-filter>
<action android:name="com.android.techrainner"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
this my MainActivity (WL : because I am using IBM MobileFirst)
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
WL.createInstance(this);
WL.getInstance().showSplashScreen(this);
WL.getInstance().initializeWebFramework(getApplicationContext(), this);
Intent startFirstServiceIntent = new Intent(this,FirstService.class);
Intent startSecondServiceIntent = new Intent(this,SecondService.class);
startService(startFirstServiceIntent);
startService(startSecondServiceIntent);
}
this is the first service :
class FirstService extends Service {
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent,int flags,int startId){
return START_STICKY;
}
}
the second service is the same as the first
the first receiver :
public class FirstServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context,FirstService.class));
}
}
Also the second receiver is the same as the first.
Two services are running on background only when the app is opened, and they will be stopped when I close it.
As in Android documentation, it is written about Service as:
A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.
So when operation is done Service stops automatically. If you want to start your service even your app gets destroyed then you can use Alarm Manager Service. You can set a repeating alarm in your application to start your service in background.
And if your requirement is to sync your app with your app server and want to sync with server even your app is closed then you can use SyncAdapter Framework of Android.
I've created a geo-location app that collects location information of the device and compares it to a pre-defined lat/long coordinates to perform certain actions when they match.
Currently I have an activity page where the users can enter the coordinates as well as other parameters such as radius and interval for polling. Also, the app starts only when the user starts it.
I wish to convert it into a service that does the following
runs in the background
parameters are read from a configuration file (this part is already done) hence no need for any main activity (no UI)
starts up automatically (probably need an android.intent.action.BOOT_COMPLETED BroadcastReceiver)
How do I do this?
Thanks.
You have to create a service for your application, like this:
ExampleService.java:
public class ExampleService extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.d(TAG, "onStart()===>In");
// Write your application logical code here
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
BootCompleteReceiver.java
public class BootCompleteReceiver extends BroadcastReceiver {
private static final String TAG = "[BootCompleteReceiver]";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Boot complete", Toast.LENGTH_LONG).show();
Log.d(TAG, "Boot complete");
// Start the ExampleService
Intent service = new Intent(context, ExampleService.class);
context.startService(service);
}
}
In Android Manifest:
<!-- Server as a service -->
<service
android:name="com.example.ExampleService"
android:enabled="true" >
</service>
<!-- Boot complete receiver -->
<receiver
android:name="com.example.BootCompleteReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
You also have to create a activity to start the service manually and awake the service from the activity.
My target Android is 4.1.2. I created an simple android service which will show Toast on boot. But this application should not have any GUI. I was success running this service only from an activity which show GUI on start.
public class MyServices extends Service {
private MediaRecorder recorder = null;
#Override
public IBinder onBind(Intent intent) {
return null;
}
public int onStartCommand(Intent intent, int flags, int StartId)
{
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
}
You can start this service from RebootReceiver but 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.
Reboot Receiver -> Android BroadcastReceiver on startup - keep running when Activity is in Background
First you have to create a receiver:
public class BootCompletedReceiver extends BroadcastReceiver {
final static String TAG = "BootCompletedReceiver";
#Override
public void onReceive(Context context, Intent arg1) {
Log.w(TAG, "starting service...");
context.startService(new Intent(context, MyServices.class));
}
}
Then add permission to your AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and register intent receiver:
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
After this is done, your application (Application class) will run along with services, but no Activities, don't put your application on SD card (APP2SD or something like that), because it has to reside in the main memory to be available right after the boot is completed.
I need my app to start running (in the background) after rebooting the device. Here's what I've come up with till now (after taking a lot of help from here...)
This is my BootUpReceiver making use of broadcastreceiver:
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, RebootService.class);
serviceIntent.putExtra("caller", "RebootReceiver");
context.startService(serviceIntent);
}
}
This is the service class:
public class RebootService extends IntentService{
public RebootService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
protected void onHandleIntent(Intent intent) {
Intent i = new Intent(getBaseContext(), MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String intentType = intent.getExtras().getString("caller");
if(intentType == null)
return;
if(intentType.equals("RebootReceiver"))
getApplication().startActivity(i);
}
}
THis is my android manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".BootUpReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name=".RebootService"/>
</application>
The problem is, When I install this on my phone and reboot, the app crashes: It says, "Transfer has stopped working". After I press the OK button, when I check the app info, the app is running.
I'm new to android and I'm not sure what is going on. Am I supposed to add any more permissions?
Kindly help.
TIA
I think your problem lies with your RebootService constructor. When the system invokes it, it doesn't provide any arguments, so it's going to crash. If you look in the logs you'll probably see something to the effect of "Unable to instantiate service..."
Try replacing your constructor with:
public RebootService() {
super( "Reboot Service" );
}