Run service after device was restarted - android

I want that one of my services runs even if user has restarted device. I've tried this code but didn't succeed. What should I do? Help me please.
Thank you!
MyService2.class:
public class MyService2 extends Service {
private MyReceiver mR = null;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
mR = new MyReceiver();
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
registerReceiver(mR, intentFilter);
}
}
MyReceiver.class
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
context.startService(new Intent(context, MyService.class));
context.startService(new Intent(context,MyService2.class));
}
}
}

You are Registering your BroadcastReciver in your Service , Which your trying to start using your BroadcastReciver which is not registered.
So try registering your broadcast Reciever in your manifest file like this
<receiver android:name="your broadcast class">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter

First no need to register your receiver in service. Add your broadcast receiver in Manifest.
Like :
<receiver android:name="your broadcast class">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
Remember to add permission also :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
When device is rebooted your receiver will automatically get executed by system as it is declared in manifest. In your receiver just start your service.
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
//add a log or toast to confirm the receive
context.startService(new Intent(context, MyService2.class));
}
}
}
AND
public class MyService2 extends Service {
private MyReceiver mR = null;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// do what ever you wanted to do...
}
}

Related

How do I start app on boot and allow run like whatsapp

i want when boot run app server allow for receive message any time
without showing application
this code problem when boot show Notification twice only
but i want receive message any time
Is this "android:exported" important, What used
code AndroidManifest
<service
android:name=".appService"
android:exported="true"
android:enabled="true">
</service>
<receiver
android:name=".ServiceStarterBoot"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
code BroadcastReceiver
public class ServiceStarterBoot extends BroadcastReceiver {
private Context context ;
#Override
public void onReceive(Context context1, Intent intent) {
this.context = context1;
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceLauncher = new Intent(context, appService.class);
context.startService(serviceLauncher);
}}
code Service
public class appService extends Service {
#Override
public void onCreate() {
super.onCreate();
lood();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
//return mBinder;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "onStartCommand", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
private void lood(){
SystemClock.sleep(3000);
Runnable runnable = new Runnable() {
public void run() {
boolean p= true ;
while(p) {
SystemClock.sleep(5000);
showNotification();
}
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
please help
thanks
you have to make your service sticky:
Service.START_STICKY
then it is infinite and restarts itself when killed by OS for any reason.
It even starts itself when booting OS and app is NOT opened by user.
Best regards.
pseudo code:
onStartCommand(..){
...
//e.g. wifilisterner
Wifilistener WF = new Wifilistener(Interface yourInterfaceCallBackMethod);
}
something like this.

BroadCast Receiver onReceive not called

I am trying to send a broadcast from a service but it is not received in the receiver. Following is my code
Bluetooth Service
public class BluetoothService extends Service {
public final static String TAG = "com.example.linvor.BluetoothService";
Intent intent1 = new Intent(TAG);
static boolean isRunning = false;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(final Intent intent, int flags, int startId) {
android.os.Debug.waitForDebugger();
super.onStartCommand(intent, flags, startId);
//android.os.Debug.waitForDebugger();
sendBroadcast(intent1);
return START_NOT_STICKY;
}
}
My Broadcast receiver is as follows.
ServiceBroadCastReceiver
public class ServiceBroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
MainActivity.myLabel.setText("BroadCAst received");
if(intent.getAction().equals(BluetoothService.TAG)) {
Log.i("Broadcast status","BroadCast received");
}
}
}
and my manifest declaration seems like this.
manifest
<receiver
android:name=".ServiceBroadCastReceiver"
android:label="#string/app_name"
android:exported="true">
<intent-filter>
<action android:name="com.example.linvor.BluetoothService"></action>
</intent-filter>
</receiver>
One more thing:
when my service is running and sends broadcast to my activity(which is not received), my app shows app not responding.
Thanks to #KeLiuyue and #Mike M. for suggesting me solutions. But the problem was not in the broadcast code. The problem was in the service which was causing my app to not respond and hence I was getting problems with sending and receiving broadcasts in my app. And the reason for my service to not respond was this line.
android.os.Debug.waitForDebugger();
I just removed this line and everything is working fine.
Try this.
registerReceiver(receiver,new IntentFilter(BluetoothService.TAG));
Intent intent = new Intent();
//edited here ,your action
intent.setAction(BROADCAST_ACTION);
//send
sendBroadcast(intent);
Edited
// edited here , add your service
Intent startIntent = new Intent(this, BluetoothService.class);
startService(startIntent);
Note
register in AndroidManifest.xml or in the java code
then sendBroadcast in the java code
unregister receiver in onDestroy() method
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}

Android - Restrict to launch

I found really interesting option in Lenovo K5
When this option is active, my background services doesn't activated after restart of the device. I searched everywhere in the web, and I set some tweaks in the manifest, but nothing helps.
I notice that in some apps, that option is not activated, like Facebook or Viber. So when they can made it, I'm sure that it have some workaround of the problem.
Thanks in advance.
MyService.java
public class MyService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startid) {
// some logic
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
}
}
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// some logic
}
}
MyAutoStart.java
public class MyAutoStart extends BroadcastReceiver {
/**
* Listens for Android's BOOT_COMPLETED broadcast and then executes
* the onReceive() method.
*/
#Override
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, ServiceMaps.class);
context.startService(intent);
}
}
Manifest.xml
<receiver android:name=".MyReceiver" />
<receiver
android:name=".MyAutoStart"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name=".MyService" />

Service not getting called on Phone startup - Android

I have tried this
Trying to start a service on boot on Android
Also I have checked with http://blog.vogella.com/2011/12/11/automatically-starting-services-in-android-after-booting/
yet my app wont restart on Phone startup. Here's the code.
AndroidManifest.xml
<service android:name=".MyService">
</service>
<receiver android:name=".Autostart"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
MyService
public class MyService extends Service implements LocationListener {
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
#Override
public void onCreate() {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
#Override
public void onDestroy() {
// Cancel the persistent notification.
this.stopSelf();
// Tell the user we stopped.
}
MainActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent(getBaseContext(), MyService.class));
Am I going wrong anywhere?
Edit:
public void onReceive(Context arg0, Intent arg1)
{
Intent intent = new Intent(arg0,MyService.class);
arg0.startService(intent);
Log.i("Autostart", "started");
}
This is my BroadcastReceiver.
Yet no results.
try register BroadcastReceiver for ACTION_BOOT_COMPLETED. The you can recieve startup broadcast.
public class BootBroadCast extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent service=new Intent(context, MyService.class);
context.startService(service);
}
}
second you need add this in AndroidManifest.xml
<receiver android:name=".BootBroadCast" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
I have followed the article at vogella and was able to get my service started at Boot.
First of all, you should have a BroadcastReceiver for the event android.intent.action.BOOT_COMPLETED. Just follow the steps from the blog. It seems you have started the service on your app's 'onCreate'. This will get invoked only if the user starts your app.
When the system boots up, your receiver class's onReceive() method will be invoked. Here is where you should start your activity as below.
public class BootReceiever extebds BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, YourService.class);
context.startService(service);
}
}
From the research that I have done, it is better to start your service after a short delay as many applications / services may start at boot, so chances of low memory are more. I started my service after a small delay.

run app in background without activity alerts user when Contact or sms is deleted

i want to make my app to be run in background and listens for
contact,sms deletion events.
for that i created a service in my app but i dnt how to start without activity
my code is like this
public class DeleteService extends Service {
ContentResolver cr;
MyContentObserver observer=new MyContentObserver();
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
#Override
public void onCreate() {
cpath=ContactsContract.Contacts.CONTENT_URI;
// some action
}
#Override
public void onDestroy() {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Launch a background thread to do processing.
super.onStartCommand(intent, flags, startId);
cpath=ContactsContract.Contacts.CONTENT_URI;
cr=getContentResolver();
cur=cr.query(cpath, null, null, null, null);
this.getApplicationContext().getContentResolver().registerContentObserver(cpath, true, observer);
return Service.START_STICKY;
}
private class MyContentObserver extends ContentObserver {
public MyContentObserver() {
super(null);
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
nfm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int NOTIFICATION_ID = 1;
Intent intent1 = new Intent();
PendingIntent pi = PendingIntent.getActivity(DeleteService.this, 1, intent1, 0);
nf=new Notification(R.drawable.ic_launcher,"Contact Database changed",System.currentTimeMillis());
nf.setLatestEventInfo(getApplicationContext(), "Delete Event", "contact name", pi);
nf.flags = nf.flags |
Notification.FLAG_ONGOING_EVENT;
}
#Override
public boolean deliverSelfNotifications()
{
super.deliverSelfNotifications();
return true;
}
}
public class LocalBinder extends Binder {
DeleteService getService() {
return DeleteService.this;
}
}
}
register ACTION_SCREEN_ON or ACTION_USER_PRESENT broadcast recivers for your Appliction in Service and start Service when screen is on or user is present. you can register ACTION_SCREEN_OFF broadcast reciver for stoping Service when phone screen is off to avoid battery drain by your app.as:
In manifest.xml:
<receiver android:name="com.my.AppStart">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
BroadcastReceiver :
public class AppStart extends BroadcastReceiver {
public static final String present = "android.intent.action.USER_PRESENT";
public static final String screenon = "android.intent.action.SCREEN_ON";
public static final String screenoff = "android.intent.action.SCREEN_OFF";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(present) || intent.getAction().equals(screenon) )
{
Intent i=new Intent(context,DeleteService.class);
context.startService(i);
}
if (intent.getAction().equals(screenoff))
{
//STOP YOUR SERVICE HERE
}
}
}
A service can only by started by an Activity, or a BroadCast receiver, or a service which is already started. It can't be stand-alone(It can't start by itself). So, you would need one of the two components to start it. you can make an activity which starts the service which is the preferred way. But if you don't want to provide a user interface, implement a broadcast receiver which fires up when the phone is switched on and the boot up is completed, Inside that br, start your service. This will also help you run the service as soon as a phone starts.
for example in your manifest:
<receiver android:name="com.my.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and in the br:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i=new Intent(context,DeleteService.class);
context.startService(i);
}
}
In your activity .. put this code in oncreate
Intent svc=new Intent(youractivity.this,DeleteService.class);
startService(svc);

Categories

Resources