background service has stopped on miui rom - android

I am developing a background services and in OnDestroy Method, I've called an intent to start my services again. I'ts not started again on miui rom (Xiaomi mobile and huawei mobile).
How do I handle this?
public class NotificationsService extends Service {
#Override
public void onCreate() {
ApplicationLoader.postInitApplication();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Intent intent = new Intent("example.app.start");
sendBroadcast(intent);
}
}
In Manifest:
<receiver android:name=".AppStartReceiver" android:enabled="true">
<intent-filter>
<action android:name="example.app.start" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

It doesn't new on Xiaomi because Xiaomi has a feature called app permission, where user has to allow the app to start automatically (Service).
Go like this and allow your app to autostart:
Settings > permissions > Autostart
Or,
Don't try to restart the same Service inside onDestroy() instead use START_STICKY inside onStartCommand(Intent intent, int flags, int startId) method.
Again you are sending broadcast not starting a service, Use onDestroy properly:
#Override
public void onDestroy() {
Intent intent = new Intent("example.app.start");
sendBroadcast(intent);
super.onDestroy();
}

Related

Creating a daemon like service for android application

I want to implement this scenario for my application. I want to schedule my service to start when the phone boots, and whenever another application calls my service I want my service to start a certain activity within the project.
So in order to be clear. I want to create a project which contains a service which runs whenever the phone boots, and is dormant, listening for a call from a third party application. And whenever that call is received this service calls an Activity (from the same project, not third party)
How can I configure my manifest file in order to achieve this?
I have also come across this suggestion but my scenario is pretty different.
Thank you very much in advance
**Define Service in manifest and Create the BroadcastReciever with boot complete permission and listen the intent.If boot completed start the service.**
public class MyService extends Service {
Context context = this;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
Intent activity = new Intent(context, MyActivity.class);
activity.putExtra("Message", "fromService");
activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(activity );
} catch (Exception e) {
MyLog.printException(e);
}
return super.onStartCommand(intent, flags, startId);
}
}
By creating a BroadcastReceiver you can perform the service startup.
public class StartupReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, ShowActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
and in the manifest
<receiver
android:name=".StartupReceiver"
android:enabled="true"
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.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
This allows you to run an Activity, you can then start a Foreground Service from that activity. I just set this example because I have it ready, you can adapt it to run a service as you like.

IntentService on Android run in background

I need to have a service in background working on Android.
I made this service:
public class ServizioBackgrounds extends IntentService {
public static int DEFAULT_PORT = 35500;
private static int BUF_SIZE = 11;
private static ....
private static ....
public ServizioBackgrounds() {
super("ServizioBackground");
}
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("##### Servizio Attivato #####");
super.onStart(intent, startId); // If I don't use, the onHandleIntent method ins't call
return START_STICKY;
}
public void onDestroy(){
System.out.println("### Servizio Terminato ###");
Intent startService = new Intent("com.perseusgalaxia.interphone_citofono"); // Try.. But this method is never called
startService.putExtra("AccendiServ", "AccendiServ");
sendBroadcast(startService);
}
#Override
protected void onHandleIntent(Intent intent) {
System.out.println("##### SIAMO DENTRO #####");
DatagramSocket socketAttesa = null;
System.out.println("*** IL SERVIZIO E' ATTIVO ***");
while(true){
...
}
}
}
Then I have this BroadcasterReceiver:
public class BootCompletedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("*** Broadcast Ricevuto ***");
context.startService(new Intent(context,ServizioBackgrounds.class));
}
}
And this is the manifest:
<service android:name=".ServizioBackground"
/>
<receiver android:name=".BootCompletedReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="com.perseusgalaxia.interphone_citofono" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This is the part where I start the service for the first time:
Intent startService = new Intent("com.perseusgalaxia.interphone_citofono");
startService.putExtra("AccendiServ", "AccendiServ");
sendBroadcast(startService);
I call the service in this way just for try.
The problem is that if I start from the app activity the service it start and works fine, but when I close the app the service is close too.
The amazing thing is that if I restart the smartphone the service start in background (without any activity shown etc.) and it works fine. Then (after reboot) if I start the app and close them the service come be closed!
Sorry for my english.
Thank's for answer.
To run your service in the background you need to call startForeground from your service.
Here's an example:
int serviceId = 101;
NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext());
b.setContentTitle("Working...")
.setContentText("I'm working!")
.setSmallIcon(R.mipmap.ic_launcher);
startForeground(serviceId, b.build());
Note that if your service runs in the background you must show a notification, so the user is notified about your work.

Run service after close application in recent app (android)

I have a service, when application is running have not problem but when close application from recent app service close too but when I test in emulator I have not this problem, (service running after I close app in recently app), I test application in Lenovo tablet with android 4.4.2 have this problem .search in google and this site all answers Told should set START_STICKY or NO_STICKY and ... but not work in the actual device, please help
public class pservices extends Service {
protected void onCreate(Bundle savedInstanceState) {
Toast.makeText(this, "Service created.", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started.", Toast.LENGTH_LONG).show();
return START_STICKY;
}
#Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed.", Toast.LENGTH_LONG).show();
}
}
U can try to declare ur service in manifest.xml by using android:process.
2 type of this attrs: ".xxx" and ":xxx".
eg.
<service
android:name="com.xxx.xxxservice"
android:enabled="true"
android:process=":remote">
<intent-filter>
<action android:name="com.xxx.xxxservice" />
</intent-filter>
</service>
It will create an independent process for ur service.

Can i start an android service without activity or GUI?

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.

How to run a service in 4.0?

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.

Categories

Resources