Autostart android service - java

How to automatically start a service in Android 3.x, the test tabblet is a Samsung Galaxy 10.1. My code works on a noname tabblet with android 2.2.1 The code works nor in android emulator with the android version 3.x
Code:
StartAtBootService.java
package test.autostart;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class StartAtBootService extends Service
{
public IBinder onBind(Intent intent)
{
return null;
}
#Override
public void onCreate()
{
Log.v("StartServiceAtBoot", "onCreate");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("StartServiceAtBoot", "onStartCommand()");
return START_STICKY;
}
#Override
public void onDestroy()
{
Log.v("StartServiceAtBoot", "onDestroy");
}
}
StartAtBootServiceReciver.java
package test.autostart;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartAtBootServiceReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("test.autostart.StartAtBootService");
context.startService(i);
}
}
}
Manifest
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name="StartAtBootService">
<intent-filter>
<action android:name="test.autostart.StartAtBootService">
</action>
</intent-filter>
</service>
<receiver android:name="StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
</manifest>

It was a SD-card issue, Eclipse install new apps on the SD-card by default on my Samsung Galaxy 10.1. To fix the issue I needed to add android:installLocation="internalOnly" in the manifest.
The new Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.autostart"
android:versionCode="1"
android:versionName="1.0" android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name="StartAtBootService">
<intent-filter>
<action android:name="test.autostart.StartAtBootService">
</action>
</intent-filter>
</service>
<receiver android:name="StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
</manifest>
I hope this will help somone in the futre.

Make sure your application is not on SD. I your application needs to run on boot time, then do not place it on SD.

Related

service and manifest files

i wrote this code in android studio , it wont show me the toast message :
here is the manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BoradCastReciever"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
</application>
</manifest>
and here is the java code class reciever :
package com.mohapp.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BoradCastReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean is_on = intent.getBooleanExtra("air", false);
if (is_on == true) {
Toast.makeText(context, "Air Plane mode is on", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, "Air Plane mode is false", Toast.LENGTH_LONG).show();
}
}
}
i am using API 33 with Gradle version 7.5 and Embedd JDK(jdk 11)
In API 30 and plus , it uses the java code more than manifest file and xml files
use onStart and onStop method and add intent inside and use this.register(obj claa of reciever) and unregister on stop method

App Service not starting when phone boots up

I have made a simple service in android studio which should start as soon as I boot the phone. It should display atleast a TOAST Message. I am using Redmi note 4 as emulator and the service is not starting when I boot or reboot the phone. I have set the app to autostart also in settings.
Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dilip3.myapplication" >
<!-- Permission for starting app on boot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Service required starting app on boot -->
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:enabled="true"
android:name=".BootService"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
</manifest>
BootService.java
package com.example.dilip3.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BootService extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Toast.makeText(context, "Boot Completed", Toast.LENGTH_SHORT).show();
Intent serviceIntent = new Intent(context, MyService.class);
context.startActivity(serviceIntent);
}
}
}
MyService.java
package com.example.dilip3.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent,flags,startId);
}
}
MainActivity.java has no changes.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
You need to start a service, not an activity.
From
context.startActivity(serviceIntent);
To
context.startService(serviceIntent);

Keep connectivity broadcast receiver running after application is closed

When I need to use Internet connection and it is not posible I want to set a broadcast receiver which keeps working if the app is closed.
I am using a service for that, but that it is not working. It works fine when the app is on screen or paused but it stops working if I close it. I am not pretty sure if I should use other thing or if I am doing something wrong.
This is my code:
package com.example.ana.exampleapp;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
public class TestsService extends Service {
private static BroadcastReceiver networkChangeReceiver;
private static String TAG = "Service";
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
Log.v(TAG, "Service created");
boolean keep = needTokeepWaiting()
if(!keep)
stopSelf();
}
#Override
public void onDestroy() {
if (networkChangeReceiver != null)
unregisterReceiver(networkChangeReceiver);
networkChangeReceiver = null;
Log.v(TAG, "Service destroyed");
}
private void registerNetworkChangeReceiver() {
networkChangeReceiver = new BroadcastReceiver() {
private String TAG = "NetworkReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "Connection changed received");
boolean keep = needTokeepWaiting()
if(!keep)
stopSelf();
}
};
IntentFilter filter = new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkChangeReceiver, filter);
}
}
And my manisfest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ana.exampleapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity"></activity>
<activity android:name=".FinishRegisterActivity"></activity>
<activity android:name=".TestActivity"></activity>
<activity android:name=".InformationActivity"></activity>
<service android:name=".TestsService"></service>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
And in the TestActivity when it is necessary I do:
Intent service = new Intent(this, TestsService.class);
startService(service);
What I am doing is similar to what it is suggested here, but it doesn't work:
Keep broadcast receiver running after application is closed
I have also tried to do it without a service and registering in the manifest, but It didn't work neither:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ana.exampleapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity">
</activity>
<activity android:name=".FinishRegisterActivity" >
</activity>
<activity android:name=".TestActivity">
</activity>
<activity android:name=".InformationActivity" >
</activity>
<receiver android:name=".NetworkChangeReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
I have just found the problem. There is a bug in Android 4.4.x which kills background services when closing the app. I was testing my app in Android 4.4.2. Here there is a detailed explanation:
http://www.androidpolice.com/2014/03/07/bug-watch-stopping-apps-on-android-4-4-2-can-silently-kill-related-background-services-a-fix-is-on-the-way/
So my code was right as I have tried in Android 4.2.2 and it works (I have tried overriding onStartCommand() so maybe that was needed).
If you don't want your service to stop when the app is closed, you should override onStartCommand() method doing something like this:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_REDELIVER_INTENT;
}
You can return START_STICKY or START_REDELIVER_INTENT, whatever works better for you

Android service crashing on boot

I have an android foreground service that is supposed to start running when the device boots up. At the boot the banner rolls by signaling that the service has started, however the banner does not stay in the notification bar after the initial notification runs buy. Additionally the service does not continue to run.Below is my manifest, broadcast receiver, and service.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.copyright.Going_Plaid"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
<application
android:allowBackup="true" android:label="Going_Plaid"
android:icon="#mipmap/ic_launcher" android:theme="#style/AppTheme">
<receiver android:name="com.example.copyright.Going_Plaid.autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity
android:name="com.example.copyright.Going_Plaid.MainActivity"
android:label="Going_Plaid">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name="com.example.copyright.Going_Plaid.Scan"
android:exported="false"
android:enabled="true"
/>
</application>
autostart.java:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class autostart extends BroadcastReceiver {
public void onReceive(Context arg0, Intent arg1){
Intent intent = new Intent(arg0, Scan.class);
arg0.startService(intent);
Log.v("autostart", "service should be started");
}
}
Scan.java:
public IBinder onBind(Intent mServiceIntent){return null;}
#Override
public void onCreate(){
startInForeground();
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
#Override
public int onStartCommand( Intent mServiceIntent,int flags, int startId) {
executorService.execute(new Runnable() {
Most of the code has been redacted because I did not think it was relevant. The service does run fine when started through the UI but runs into issues when starting from boot without the user manually starting it. If you need any other information I would be happy to add it. Thank you in advance for your assistance.

Background service doesn't work

I am trying to create an Android app in Android Studio.
The app is mainly a background service to send some information to the server around every 15 seconds or so.
I have created an empty activity, boot receiver and a service class, but it doesn't work.
Could somebody help me, and explain why it doesn't work.
The relevant files:
MainActivity.java
package nl.robinvandervliet.robinsapp.app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
finish();
}
}
BootReceiver.java
package nl.robinvandervliet.robinsapp.app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
context.startService(new Intent(context, RobinsService.class));
}
}
RobinsService.java
package nl.robinvandervliet.robinsapp.app;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class RobinsService extends Service
{
private Runnable runnable = new Runnable()
{
#Override
public void run()
{
//Opening connection to the server.
while (true)
{
//Sending some data to the server.
//Sleeping for around 15 seconds.
}
//Dead code, should never reach this place.
}
};
public IBinder onBind(Intent intent)
{
return null;
}
#Override
public void onCreate()
{
new Thread(runnable).start();
Notification notification = new Notification.Builder(getApplicationContext()).setContentTitle("MyService is running!").setContentTitle("(more information later)").build();
startForeground(1, notification);
Toast.makeText(getApplicationContext(), "Service created!", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.robinvandervliet.robinsapp.app" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="nl.robinvandervliet.robinsapp.app.MainActivity"
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="nl.robinvandervliet.robinsapp.app.BootReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="nl.robinvandervliet.robinsapp.app.RobinsService" android:exported="false" />
</application>
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
Thanks, Robin.
Since you haven't implemented some parts yet I assume you just want it to run when you boot your device. Maybe take a look here. Adding an extra action in the manifest might solve your problem.

Categories