Issues with a service running on a different process - java

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).

Related

How to detect incoming and outgoing calls?

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?

Android - Start a service on boot not working

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.

Android Start service at specific time using AlarmManager and also device boot complete

I wont start service at specific time everyday and also start when the device boot complete.
For example.. at 13.00 Pm everyday the service started and show a Toast ("Service started").
not only that, but the service has to start also at boot complete but if you are not the 13.00 pm should not show the toast but must started
For boot complete define receiver for ACTION_BOOT_COMPLETED
Start service at boot complete
For Specific time, use AlarmManager
Start Service At Specific Time
public class onBootComplete extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
//Do your task here
}
}
}
And in Manifest declare this..
<receiver
android:name=".onBootComplete"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="500" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Add this permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
For timed actions use AlarmManager
This class provides access to the system alarm services. These allow
you to schedule your application to be run at some point in the
future. When an alarm goes off, the Intent that had been registered
for it is broadcast by the system, automatically starting the target
application if it is not already running.
check this link ,it will help you How to repeat notification daily on specific time in android through background service

Need to start app immediately after the boot completed

I need to know why my app didn't run immediately after booting in android real phone? My app runs but after a few second of booting.
My Code is
public class AutoStart extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
My activity is running, but after few Seconds of the boot completed. Is it Possible to reduce this few second?
I want to run my app immediately. I Don't want to allow user to access the phone.
This can increase you priority but still there would be some delay. Since android first load its OS and the all the other activity starts.
<receiver
android:name=".AutoStart"
android:enabled="true"
android:exported="true"
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I run through this problem too. To anyone still searching for solution:
I want to run my app immediately. I Don't want to allow user to access the phone.
Consider turning your app into home launcher. Make changes in the manifest:
Add to your activity
android:launchMode="singleTask"
Add to intent filter in activity
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
After that it will launch immediately with system, not showing to user anything else.
Android system does lot of work on boot completed.
hence the intent might be delayed. Depending on the phone capabilities, the intent delay times will vary.

Start service at boot time for android 4.0

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);
}
}

Categories

Resources