I am trying to write a service that runs on phone boot, and must read data off the SD card. At first I was using a reciever for android.intent.action.BOOT_COMPLETED but switched to the intent below to make sure that the SD card has been loaded.
My Issue is that on a my Nexus 7, it doesn't appear to receive the MEDIA_MOUNTED intent. The Nexus 7 doesn't have an SD card (but it has separate SD card partition). I also tried the BOOT_COMPLETED intent, with the same luck. I have tested the same code on the emulator and my Thunderbolt, and both intents work.
Manifiest:
<receiver
android:name=".StartupReceiver"
android:enabled="true"
android:exported="true"
android:label="Start the NFS Automounter Service">
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"></action>
<data android:scheme="file"/>
<!-- <action android:name="android.intent.action.BOOT_COMPLETED"></action>-->
</intent-filter>
</receiver>
The BroadcastReceiver class:
public class StartupReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
//if ("android.intent.action.MEDIA_MOUNTED".equals(intent.getAction()))
//{
Log.d("NFS_Automounter", "Recieved Mount");
Intent serviceIntent = new Intent("com.ancantus.nfsautomounter.AutomountService");
context.startService(serviceIntent);
//}
}
}
I commented out the intent matching just to try and log if the class is executed at all.
My only hunch is that the Nexus 7 doesn't broadcast a MEDIA_MOUNTED because it doesn't have a real SD card; but I can't receive the BOOT_COMPLETED intent either.
And to forstall the question; yes I do have the BOOT_COMPLETED permission.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
How many times must I type in this answer before it starts coming up in enough search results that people will find it? Maybe boldface caps will work:
STARTING WITH ANDROID 3.1, NO BroadcastReceiver WILL WORK, AT ALL, UNTIL SOMETHING HAS MANUALLY RUN ONE OF THE APPLICATION'S OTHER COMPONENTS, SUCH AS A USER RUNNING AN ACTIVITY.
This is in the documentation (albeit not well located), in blog posts, and in many StackOverflow answers, such as:
https://stackoverflow.com/a/9084771/115145
https://stackoverflow.com/a/11865858/115145
https://stackoverflow.com/a/11744499/115145
So, add an activity to your app. You need some activities anyway, for settings to control your background operation, for your documentation, for your license agreement, for your privacy policy, etc.
(note: I'm not really yelling at you -- I am just frustrated that this keeps coming up despite efforts to get the word out...)
Please note that many Android devices emulate SD card in the way it does not affect access to the SD card even when desktop accesses it. Therefore it may be that Nexus 7 simply exposes all memory that way, so as it does not really mount anything, it'd not broadcast MEDIA_MOUNTED. If you want to do some tasks on boot, listening to BOOT_COMPLETED is the only correct approach.
Related
I am working on a client app. He want to give android cellphones to his employees. With our app already installed. In our app he wanted to get the record of every phone call.
I know how to do it, I made the BoradcastReceiver. But it is only working in the older version. New Version like Android Nougat and Pie has restriction on it. And I do not any working solution of catching call event.
So Here is what I am doing in my app that is working on older version.
<receiver android:name=".MyCallReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
public class MyCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Call Event",Toast.LENGTH_LONG).show();
}
}
Solution I found:
So on older version I am watching this toast. but same code not
working on the new versions. I Have read that we need to declare same
broadcast in code.
So on above R&D I have some confusion
Confusion: When declaring in code, will it work even my app is not running at all?
Another Question:
Also we want to track the other calls? is it possible like call of
whatsapp and viber?
Please give me detail answer of what to do in case 1 and is it even possible to track calls of whatsapp and viber andother apps like this?
In my app, I'm trying to start a service on phone boot. But it's not responding at all.
public class ServiceStarter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, AppServices.class);
context.startService(pushIntent);
}
}
}
In the manifest, I did this.
<receiver android:name=".ServiceStarter" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" tools:ignore="BatteryLife" />
</intent-filter>
</receiver>
Inside the service AppServices.class
onCreate {
Toast.makeText(getAppContext(),
"Phone booted", Toast.length_long).show(); //just for test
andMyOtherCodeAsWell();
}
But it's not working at all, can anyone help me with the issue?
SOLVE working after 15 secs of the boot even the app is not running in the background(manually cleared by user).
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Check your API version. Oreo behaviour changes - If you attempt to call startService() when your application is not in the foreground then an IllegalStateException will be thrown.
Docs :
Context.startForegroundService() method starts a foreground service.
The system allows apps to call Context.startForegroundService() even
while the app is in the background. However, the app must call that
service's startForeground() method within five seconds after the
service is created.
so call:
context.startForegroundService() in your BroadcastReceiver and promote your service to a foreground service within 5 seconds of it starting by showing a notification i.e.: startForeground(NOTIFICATION_ID, notification);
Also make sure you have the correct permissions:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
You can try to put a breakpoint inside the BroadcaseReceiver and then send BOOT_COMPLETED broadcast via adb with the following command:
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -p com.example.package
(Don't forget to change com.example.package to your package name).
This way you can check and see if your broadcast receiver is getting called.
I am trying to start a service in a new process, so it would stay alive when the app closes.
I have an activity called MainScreen, and an IntentService called BackgroundSensorService.
Here is the manifest definition of the service:
<service
android:name=".services.BackgroundSensorService"
android:exported="false"
android:process=":backgroundSens" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
</intent-filter>
</service>
Here is the code snippet that runs the service:
Intent intent = new Intent(MainScreen.this, BackgroundSensorService.class);
intent.setAction("android.intent.action.SEND");
startService(intent);
When I try to set a breakpoint in the HandleIntent method, I never reach it.
I tried to set a breakpoint in onCreate, but I never reach that one either.
The weird this is, if I remove the 'process' tag from my service, everything works perfectly.
I am breaking my head over this issue...
Note: I am trying to mimic the behavior of the whatsapp sample service, that keeps track of incoming messages even while the app is closed. The service should run in the background, and have no GUI.
I got a working example of a unbound service running after the application is closed here:
https://github.com/kweaver00/Android-Samples/tree/master/Location/AlwaysRunningLocation
Android Manifest code in application tag:
<service
android:name=".YourService"
android:enabled="true"
android:exported="true"
android:description="#string/my_service_desc"
android:label="#string/my_infinite_service">
<intent-filter>
<action android:name="com.weaverprojects.alwaysrunninglocation.LONGRUNSERVICE" />
</intent-filter>
</service>
Start service:
Intent servIntent = new Intent(v.getContext(), YourService.class);
startService(servIntent);
The example here, called every time the location changed (using mock locations) and the app was not opened
In my experience with Android services, once you kill the app, the service will be killed as well. You can however force it to restart itself.
In your service, you should be using the onStartCommand method that returns the type of service you'd like to use.
The main options are:
START_NOT_STICKY: tells the OS not to recreate the service if its closed.
START_STICKY: Tells OS to restart the service if its closed (sounds like you want this one).
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
return START_STICKY; //restarts service when closed
}
When the service is restarted however, all parameters passed to it will be reset. If, like me, you need to keep track of certain data, you can use SharedPreferences to save and read values (there might be a better way, but this worked for me).
I want to write some kind of background-live-ticker app for sports-web-services...
I would like my app to be able to call the TIME_TICK all the time.
Btw: I also tried to use the AlarmManager, but the problem is the same.
But now my problem...
I use a Receiver with a Service for the execution part.
The Receiver is called every minute correctly after register.
But every night the service is terminated and will never be called again.
On Android 2.x everything works fine but Android 4.x will stop the Receiver every day...
Is there any posibility to keep the app alive on Android 4.x?
The Reveiver is registered in my Main-Activity:
registerReceiver(new MyReceiver(), new IntentFilter(Intent.ACTION_TIME_TICK));
Manifest-entries:
<service android:name="de.pepdev.MyService" />
<receiver android:name="de.pepdev.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_TICK" />
</intent-filter>
</receiver>
Receiver-class:
public class MyReceiver extends BroadcastReceiver
{
public static long nextExecTime = 0;
public static Calendar currentTime = Calendar.getInstance();
#Override
public void onReceive(Context context, Intent intent)
{
currentTime = Calendar.getInstance();
if(nextExecTime <= currentTime.getTimeInMillis())
{
Intent service = new Intent(context, MyService.class);
context.startService(service);
}
}
}
I also tried to use the AlarmManager, but the problem is the same
AlarmManager is a far better answer than ACTION_TIME_TICK, particularly if you let the user configure the polling frequency (including an option for "never poll, please, as I like my battery and bandwidth usage to stay low").
Please feel free to ask a separate StackOverflow question regarding whatever problems you feel your are experiencing with it.
But every night the service is terminated and will never be called again
Android can and will terminate your process at any point, either by user request or due to old age.
Manifest-entries:
The <receiver> is pointless, as you cannot register for ACTION_TIME_TICK via the manifest.
I can't not start service when I want to start service at boot time for android 4.0, in android.
My code is below:
> public class StartUpReceiver extends BroadcastReceiver{ #Override
> public void onReceive(Context context, Intent intent) { String
> action = intent.getAction(); if
> (action.equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
> Intent myIntent = new Intent(context, StartAUT_Service.class);
> Log.i("Broadcast", "startService on boot time:." + myIntent);
> context.startService(myIntent); }
> } }
> <uses-permission
> android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
> <application android:icon="#drawable/icon" android:label="#string/app_name">
> <receiver android:name="com.Android.Exercise.StartUpReceiver" android:exported="false">
> <intent-filter>
> <action android:name="android.intent.action.BOOT_COMPLETED"/>
> <action android:name="StartInstrument" />
> <action android:name="PrintControlName" />
> </intent-filter>
> </receiver>
> <service android:enabled="true" android:name="StartAUT_Service">
> <intent-filter>
> <action android:name="com.Android.Exercise.StartAUT_Service" />
> </intent-filter>
> </service>
> </application>
And in LogCat show Shutting down VM when i run above project, so my broadcast don't receive action.
Plz help me.!!!
Thanks so much.
Mybe this will help you:
Broadcast Regression Confirmed
In a previous post, I cited evidence that the BOOT_COMPLETED broadcast will not work out of the box on Android 3.1 until the user uses your app.
It’s actually somewhat bigger than that.
In the issue that I filed seeking clarification, Ms. Hackborn indicated:
Starting with 3.1 when applications are installed they are in a “stopped” state so they will not be able to run until the user explicitly launches them. Pressing Force Stop will return them to this state.
As a result, when applications are first installed, they are totally ignored by the system until and unless the user manually launches something: clicking on a launcher activity or adding an app widget, most likely.
Developers who had been relying upon getting some sort of system broadcast without user intervention will need to adjust their apps for Android 3.1.
As I wrote in the previous post:
I expect that most apps will be OK. For example, if your boot receiver is there to establish an AlarmManager schedule, you also needed to establish that schedule when the app is first run, so the user does not have to reboot their phone just to set up your alarms. That pattern doesnot change – it’s just that if the user happens to reboot the phone, it will not set up your alarms, until the user runs one of your activities.
UPDATE: To clarify the above quote, 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.
This change is not terribly shocking, as it ratchets up the security another notch by limiting ways malware can run without user knowledge. While it does not offer perfect security — the malware can still install its own copy of an Angry Birds launcher icon and hope users screw up — it is an improvement.
In your onResume method of a BroadCastReceiver class put following code
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, yourService.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(i);
}
}