I have stuck with an issue of running a service when force stop is clicked and when i restart my mobile the service should be invoked.I have followed some examples but i cant able to achieve the task.Can any one guide me to achieve the task.
Required:
1.Service should run when force stop has been clicked from settings
2.Service should run when mobile has been restarted.
TestActivity.java
package com.testsearching;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
startService(new Intent(this, ServiceTest.class));
}
}
ServiceTest.java
package com.testsearching;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class ServiceTest extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
mTimer = new Timer();
mTimer.schedule(timerTask, 2000, 2 * 1000);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
} catch (Exception e) {
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
private Timer mTimer;
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
Log.e("Log", "Running");
}
};
public void onDestroy() {
try {
mTimer.cancel();
timerTask.cancel();
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent("com.android.techtrainner");
intent.putExtra("yourvalue", "torestore");
sendBroadcast(intent);
}
}
ReceiverCall.java
package com.testsearching;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class ReceiverCall extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.i("Service Stops", "Ohhhhhhh");
context.startService(new Intent(context, ServiceTest.class));;
Toast.makeText(context, "My start", Toast.LENGTH_LONG).show();
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testsearching"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:theme="#style/AppTheme" >
<activity
android:name="com.testsearching.TestActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ServiceTest" >
<intent-filter>
<action android:name="com.testsearching.ServiceTest" />
</intent-filter>
</service>
<receiver
android:name="ReceiverCall"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="com.android.techtrainner" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
In theory, this is not possible; according to the Android security model.
As Panaj Kumar points out in the comments:
When user does force stop, means he does not want to run this
application (any component). he is not interested anymore, and this
is rights of user. SO android does not gives a way to keep running
your service, even after forced close your app.
Android will prevent the app from restarting using the START_STICKY flag, and will disable the RECEIVE_BOOT_COMPLETED receiver. The system will also disable all Alarms that have been set for this app.
Before the system will allow the app to run again, the user must run an Activity of the app themselves.
That said, it seems that certain apps are still able to break the rules in this way. This should be considered incorrect and would be taking advantage of a security hole, however it shows that it is still possible, even on KitKat.
The Discovery Insure driving app seems to be able to restart itself when it has been force stopped, and will restart on boot:
Discovery Insure Driving Challenge on Play Store
However, this functionality should not be relied on - hopefully this security flaw will be fixed in future system updates.
Write this on in your OnCreate of the main launching activity
if(!isMyServiceRunning(ServiceClass.class))
context.startService(new Intent(context.getApplicationContext(),ServiceClass.class));
here is the running service check function
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
and Create your service with this architecture.Observe the return START_STICKY
public class ServiceClass extends IntentService {
public ServiceClass()
{
super("null");
}
public Context context;
public Intent intent;
public Date currentTime;
#Override
public int onStartCommand(#Nullable Intent intent, int flags, int startId) {
onHandleIntent(intent);
return START_STICKY;
}
#Override
protected void onHandleIntent(#Nullable Intent intnt) {
currentTime = Calendar.getInstance().getTime();
context = getApplicationContext();
this.intent = intnt;
Log.d("ServiceClass","Service class running "+ currentTime);
}
}
Related
I am running a service from my Android test app, which needs to show toast from counter value which is continuously increasing in a service.
Its starting well, and showing the toast. But, once I press back/home button to keep the app in background, the toast stops showing. When I again bring the app to foreground, again the toast started visible.
This problem happens sin Kitkat. But in JellyBeans and below, its working fine.
Here is my manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rtrgroup.mysms"
android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="9"
android:targetSdkVersion="19"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name="com.rtrgroup.mysms.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>
<service android:name="com.rtrgroup.mysms.MyService"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>
Here is my MainActivity.java file.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onStart() {
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
}
}
Here is my service code, MyService.java:
package com.rtrgroup.mysms;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.*;
import android.widget.Toast;
public class MyService extends Service {
Handler handler;
int count = 0;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler = new Handler();
handler.postDelayed(test, 1000);
return START_NOT_STICKY;
}
Runnable test = new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "TOAST count = " + String.valueOf(count), Toast.LENGTH_SHORT).show();
count++;
handler.postDelayed(test, 5000);
}
};
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
So, how to make toast visible always, even the app is in background and the service is running? Please explain.
if you want to detect when the user put your app in background , override this method
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
switch (level) {
case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
// do your magic here
break;
}
}
check the documentation http://developer.android.com/reference/android/content/ComponentCallbacks2.html#TRIM_MEMORY_UI_HIDDEN
also you can start your service in "foreground" mode for this check
Show notification from background started service
or you can show a notification instead a toast.
I am trying to create a Service application without a UI/Activity.
The service will start on BOOT_COMPLETED. Currently I am experiencing an issue when the receiver service cannot start the main service.
The error sound like that (Android Device Monitor):
Tag: Activity Manager
Text: Unable to start service Intent { cmp=com.remote.cat/.ActionService } U=0: not found
My android OS version on the device is 4.2.2
I am testing it via this command in PowerShell:
adb.exe shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.remote.cat/.AutoStartServiceReceiver
Both services are in the root of the package com.remote.cat
Feel like I am missing a small thing or having a typo, would greatly appreciate any help!
Thank you!
Here is the code of the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.remote.cat"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:allowBackup="true" android:label="#string/app_name"
android:icon="#mipmap/ic_launcher" android:theme="#style/AppTheme">
<receiver android:name="com.remote.cat.AutoStartServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<service android:name="com.remote.cat.AutoStartServiceReceiver"></service>
<service android:name="com.remote.cat.ActionService"></service>
</manifest>
Here is the brodcastreceiver class:
package com.remote.cat;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AutoStartServiceReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent serviceIntent = new Intent(context, ActionService.class);
//Intent serviceIntent = new Intent("com.remote.cat.ActionService");
context.startService(serviceIntent);
}
}
}
Here is the main service that I am trying to start:
package com.remote.cat;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ActionService extends Service
{
private ScheduledThreadPoolExecutor executor = null;
#Override
public IBinder onBind(Intent intent)
{
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
executor = new ScheduledThreadPoolExecutor(4);
final ScheduledFuture<?> handle = executor.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "one minute message", Toast.LENGTH_LONG).show();
}
}, 0, 8, TimeUnit.SECONDS);
return START_STICKY;
}
}
Change your AutoStartServiceReceiver declaration in the manifest to be a <receiver> rather than <service>.
The error is in your broadcastreceiver class
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
it should be
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
EDIT
I believe you have to declare your services inside the application tag since it is a part of the application
I want to create an Android application which performs some action periodically (every minute) in the background. The application shall have no user interface, therefore there will be no Action-Class. It shall start automatially after boot.
I have coded my Application as shown below, but it just does not start after boot? Can anyone help?
I am using a Samsung Tablet GT-P5200 with Android 4.4.2
Many thanks for your help in advance.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.somecompany.justoneservice" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" >
</service>
</application>
</manifest>
BootReceiver.java
package com.somecompany.justoneservice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.text.format.DateUtils;
import android.util.Log;
public class BootReceiver extends BroadcastReceiver {
public BootReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "BOOT");
long interval = DateUtils.MINUTE_IN_MILLIS * 1;
long firstStart = System.currentTimeMillis() + interval;
Intent mainServiceIntent = new Intent(context, MyService.class);
PendingIntent mainServicePendingIntent = PendingIntent.getService(context, 0, mainServiceIntent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC, firstStart, interval, mainServicePendingIntent);
}
}
MySevice.java
package com.somecompany.justoneservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
public MyService() {
}
#Override
public void onCreate() {
super.onCreate();
Log.d("TAG", "Service created.");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TAG", "Service started. (" + startId + ")");
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d("TAG", "Service started.");
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
The application shall have no user interface, therefore there will be no Action-Class.
Newly-installed apps' manifest-registered receivers are disabled until something uses an explicit Intent to start one of your components. Usually, this is the user tapping on the icon for one of your activities in a home screen's launcher. And usually that is not a problem, because all apps need an activity, to allow the user to configure the behavior of the app, get help, read the license agreement terms, etc.
In some scenarios, something other than a home screen launcher icon can start up one of your components with an explicit Intent. For example, if you are a plugin to some other app, that other app might detect your installation and start up one of your components.
But, if nothing will start one of your components with an explicit Intent, then your BOOT_COMPLETED receiver will never get control, even after a reboot.
I know this question has been asked many times, but all the other threads didn't solve my issue at all, I can't see anything wrong with my code. Maybe I missed something here, can anyone help me out?
Code for the Intent Service:
package Services;
import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;
public class WifiSearchService extends IntentService {
/**
* A constructor is required, and must call the super IntentService(String)
* constructor with a name for the worker thread.
*/
public WifiSearchService() {
super("WifiSearchService");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent,flags,startId);
}
/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns, IntentService
* stops the service, as appropriate.
*/
#Override
protected void onHandleIntent(Intent intent) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
}
}
Starting the service by flicking a switch:
package com.cdobiz.wifimapper;
import Services.WifiSearchService;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
public class MainActivity extends Activity {
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
Switch serviceSwitch = (Switch) this.findViewById(R.id.switchService);
serviceSwitch.setChecked(isMyServiceRunning());
serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton view, boolean state) {
if(state){
startService(new Intent(context, WifiSearchService.class));
}else{
stopService(new Intent(context, WifiSearchService.class));
}
}
});
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
Log.v("debug", service.service.getClassName());
if ("com.cdobiz.wifimapper.services.WifiSearchService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cdobiz.wifimapper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.cdobiz.wifimapper.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>
<service android:enabled="true" android:name=".services.WifiSearchService"></service>
</application>
</manifest>
I was able to run your service by changing the package name to com.cdobiz.wifimapper.services
and change the package name of the service in the manifest.
I solved the problem by making sure that the service is registered in the App-Manifest.
I had the same problem. I solved it by copying the class content code text. Then I deleted the class and created the same class again by right clicking the package -> New -> Service -> Service(Intent)
Then I pasted the same code to the class and it works.
I Hope this will be helpfull to someone
I solved a Problem with my Intent Service not starting by using
Intent intent = new Intent(this,UploadService.class);
this.startService(intent);
to start my Service
try with android:enabled="true"in manifest if the ser
I had the same problem.
To solve it I deleted the Intenservice class, wi an than
i created a new class using: right click>new>Service>Service(IntentService)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Trying to start a service on boot on Android
BroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartActivityAtBoot extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent i = new Intent(context, CompareIMSI.class);
context.startService(i);
}
}
}
CompareSIM.java
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class CompareIMSI extends Service{
Context context;
TelephonyManager operator;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
//compareSIM();
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
compareSIM();
}
public void compareSIM(){
final String STORAGE = "Storage";
SharedPreferences unique = getSharedPreferences(STORAGE, 0);
final String storedIMSI = unique.getString("simIMSI", "");
final String currentIMSI = getSubscriberId().toString();
if (!storedIMSI.equals(currentIMSI)){
Intent i = new Intent(CompareIMSI.this, ScreenLockActivity.class);
startActivity(i);
}
}
public String getSubscriberId(){
String IMSI = null;
String serviceName = Context.TELEPHONY_SERVICE;
TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
IMSI = m_telephonyManager.getSubscriberId();
return IMSI;
}
}
I would like the application to start the compareSIM service upon boot up, during boot up, this service will run as the current attached SIM card IMSI will be retrieved and matched with the already saved IMSI, once they are different the user will be brought to a login layout. I want to perform this during boot up but failed to do so... Kindly advice me on the coding, thanks
floow these steps for stating your service on BOOT:
Step 1: In AndroidManifest.xml add BOOT_COMPLETED permission as:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
Step 2: In AndroidManifest.xml Register your Reciver as:
<receiver android:name=".StartActivityAtBoot" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
Step 3: In AndroidManifest.xml Register your Service as:
<service android:name=".CompareIMSI"> </service>
Step 3: In StartActivityAtBoot Start your service as:
public class StartActivityAtBoot extends BroadcastReceiver
{
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION))
{
context.startService(new Intent(context,
CompareIMSI.class), null);
Toast.makeText(context, "CompareIMSI service has started!", Toast.LENGTH_LONG).show();
}
}
}
This is all about Starting a Service on Boot.Thanks
You need to register the BroadcastReceiver in the Android manifest, like this:
<receiver android:name=".StartActivityAtBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Also make sure that you have this permission in the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Check Your androidManifest file. You need to add receiver at androidManifest file.
<receiver android:name=".......StartActivityAtBoot" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>