I have tried to get device activity such as device is traveling in Vehicle, bicycle, walking or running or not travelling using google play service library but ActivityRecognitionResult getting as null.
Tried out code is in Class1.java:-
public class Class1 extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
public static String ACTION_STRING= "action";
Intent intentp = new Intent(context, Class2.class);
intentp.setAction(ACTION_STRING);
pendingIntent = PendingIntent.getBroadcast(context, 0, intentp, PendingIntent.FLAG_ONE_SHOT);
}
}
And code in Class2.java:-
public class Class2 extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
if(intent.getAction() == Alarm.ACTION_STRING)
{
if(ActivityRecognitionResult.hasResult(intent)) //Here condition getting as false
{
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
DetectedActivity detectedActivity = result.getMostProbableActivity();
int activityType = detectedActivity.getType();
if(activityType != DetectedActivity.STILL)
{
//Here doing something;
}
}
}
}
}
How to detect activity of device using Google Play Services?
From the documentation:
Use of the ActivityRecognitionClient requires the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission.
It the permission properly requested by your app?
Related
I'm a bit confused because i read some posts where i'm supposed too use ContextCompat.StartForegroundService(); if the API is >= 26.
Now I still just use StartService and it works even though i'm supposed to get an IllegalStateException on an API >= 26 ( current api on phone is 27) according to this post.
https://medium.com/mindorks/mastering-android-service-of-2018-a4a1df5ed5a6
I know Service is an old concept. Let me assure you we will not discuss the basics and we will learn the recent changes made to the service layer in Android 8.0+, we will solve the mystery of famous IllegalStateException and RemoteServiceException. This article is not a conventional way of understanding services, hang tight till you can.
So my question is if i should change startForeGroundService or just keep startService for API >=26?
My Class that handles my Service connection:
/**This establishes the connection to the MediaPlayerService. */
public static ServiceConnection serviceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
MediaPlayerService.MusicBinder binder = (MediaPlayerService.MusicBinder)service;
mediaPlayerService = binder.getService();
mediaPlayerService.musicBound = true;
}
#Override
public void onServiceDisconnected(ComponentName name) {
mediaPlayerService.musicBound = false;
}
};
/**This is called to start the MediaPlayerService. */
private static Intent mediaPlayerServiceIntent = null;
public static void startMusicService(Context c) {
/*mediaPlayerServiceIntent binds our connection to the MediaPlayerService. */
mediaPlayerServiceIntent = new Intent(c, MediaPlayerService.class);
c.bindService(mediaPlayerServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
c.startService(mediaPlayerServiceIntent);
mServiceIsActive = true;
}
/**This is called to stop the MediaPlayerService. (onDestroy) */
public static void stopMusicService(Context c) {
if (mediaPlayerServiceIntent == null)
return;
c.unbindService(serviceConnection);
c.stopService(mediaPlayerServiceIntent);
mediaPlayerServiceIntent = null;
mediaPlayerService = null;
}
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Main.startMusicService(getApplicationContext());
}
startService will not work for api >=26
You can change your service to foreground service with help of following code. It will show the notification.
private void runAsForeground(){
Intent notificationIntent = new Intent(this, MediaPlayerService.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0,
notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentText(getString(R.string.isRecording))
.setContentIntent(pendingIntent).build();
startForeground(NOTIFICATION_ID, notification);
}
for more reference - https://android-developers.googleblog.com/2018/12/effective-foreground-services-on-android_11.html
https://developer.android.com/guide/components/services
another way (not recommended.. target sdk must be 26 or less)
public static void startService(Context context, Class className) {
try {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
Intent restartServiceIntent = new Intent(context, className);
restartServiceIntent.setPackage(context.getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(context, 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmService != null) {
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 500,
restartServicePendingIntent);
}
} else {
Intent i = new Intent(context, className);
context.startService(i);
}
} catch (Exception e) {
MyLog.e(TAG, "startService: ", e);
}
}
Call by
startService(context,MediaPlayerService.class);
I have an app that plays video on ACTION_SCREEN_ON. However, I do not want to play the video if the reason of screen on is an incoming call. I tried to detect incoming phones with TelephonyManager.ACTION_PHONE_STATE_CHANGED but I could not. How can manage to do this? You can find my code below.
Services.java
public class Services extends Service {
private BroadcastReceiver sReceiver;
public IBinder onBind(Intent arg){
return null;
}
public int onStartCommand(Intent intent,int flag, int startIs) {
IntentFilter filter=new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
//TelephonyManager.ACTION_PHONE_STATE_CHANGED ????
sReceiver=new Receivers();
registerReceiver(sReceiver,filter);
return START_STICKY;
}
}
Receivers.java
public class Receivers extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
Log.e("Log", "The screen is on.");
Intent i = new Intent(context.getApplicationContext(), CommercialVideo.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
else{
Log.e("Log", "The screen is off.");
}
}
}
I am developing android application, so I am starting a service with alarm:
public void scheduleLocationCheckerAlarm() {
Intent intent = new Intent(getApplicationContext(), LocationCheckerReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, LocationCheckerReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 600000, pIntent);
}
LocationCheckerReceiver:
public class LocationCheckerReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, LocationNotificator.class);
context.startService(i);
}
Service:
public class LocationNotificator extends Service {
public LocationNotificator() {
}
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Location checker", "Service running");
//My code is here
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("Location checker", "Service destroyed");
}
So I want this service to be checking for something every 1 minute and to be running all the time, even when the application is closed by the user.
You must call startForeground(FOREGROUND_ID, buildForegroundNotification(filename)); in order to ensure that your service running continuously. Also, this will post a notification from your app to show the user about the service state. Please follow the reference.
Here is the code :
public class LocationNotificator extends Service {
private static int FOREGROUND_ID=1338;
public LocationNotificator() {
}
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Location checker", "Service running");
//My code is here
startForeground(FOREGROUND_ID,
buildForegroundNotification(filename));
stopForeground(true);
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("Location checker", "Service destroyed");
}
private Notification buildForegroundNotification(String filename) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);
b.setOngoing(true);
b.setContentTitle("Some Title")
.setContentText("some File name")
.setSmallIcon(android.R.drawable.stat_sys_download)
.setTicker("downloading");
return(b.build());
}
You need to read this first. https://developer.android.com/guide/components/services.html
In a nutshell, start your service as a foreground service so there's lesser chance of Android killing your service. As a foreground service, you need to display an on-going notification in the status bar.
There's no direct way of making sure your service is never killed by the Android system. A workaround is to send a broadcast in onDestroy() of your service, and have a Receiver in your Android application start the service upon receiving the broadcast.
By the way, it seems that your service is sending location updates periodically to your backend server. This might be better implemented using Firebase Job Dispatcher library or Evernote's Android-Job library.
Please see edits before answering!
I have an app which contains a BackgroundService class:
public class BackgroundService extends Service {
#Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction("com.spotify.music.playbackstatechanged");
filter.addAction("com.spotify.music.metadatachanged");
filter.addAction("com.spotify.music.queuechanged");
registerReceiver(receiver, filter);
Log.e("Playing:", "APP IS PLAYING");
Notification notification = new Notification();
startForeground(1, notification);
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
long timeSentInMs = intent.getLongExtra("timeSent", 0L);
String action = intent.getAction();
if (action.equals(BroadcastTypes.METADATA_CHANGED)) {
String trackId = intent.getStringExtra("id");
String artistName = intent.getStringExtra("artist");
String albumName = intent.getStringExtra("album");
String trackName = intent.getStringExtra("track");
int trackLengthInSec = intent.getIntExtra("length", 0);
// Do something with extracted information...
} else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) {
boolean playing = intent.getBooleanExtra("playing", false);
Log.e("Playing:","TRUE");
}
}
};
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
static final class BroadcastTypes {
static final String SPOTIFY_PACKAGE = "com.spotify.music";
static final String PLAYBACK_STATE_CHANGED = SPOTIFY_PACKAGE + ".playbackstatechanged";
static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged";
}
}
and this is declared in my manifest:
<service
android:name=".BackgroundService"
android:enabled="true" >
<intent-filter>
<action android:name="com.spotify.music.playbackstatechanged" />
<action android:name="com.spotify.music.metadatachanged" />
<action android:name="com.spotify.music.queuechanged" />
</intent-filter>
</service>
So essentially my objective is to have my BackgroundService initialized when my app is opened, and to have it continue to run in the Background doing whatever I need it to do. As of now, I am using logs to determine whether my "setup" is working, but when I run my app, I am unable to see an logs even after I tested all actions that should have triggered my BroadCastReceiver. Furthermore, my persistent notification should have changed had my service been running, but it does not...
Edit::
So, I added logs to my BackgroundService's onCreate() and onReceive() methods, however, neither seem to be appearing. Im wondering, do I need to do something in my launcher activity to initialize the service? Furthermore, no notification is shown so I assume the Service is not being started for some reason...
Latest Edit:
So I added the following code to my Main activity to see if it would make a difference:
startService(new Intent(this,BackgroundService.class));
And after debugging my app, I began to see the following error:
java.lang.RuntimeException: Unable to create service com.aurum.mutify.BackgroundService: java.lang.SecurityException: Isolated process not allowed to call registerReceiver
pointing to my BroadCast Receiver class.
Intent services are designed for short tasks. And your intent handling method is empty.
If you need long running task in the background use standard service and call start foreground. This will minimize chance of system destroying your service.
To learn more go here
EDIT
Try overriding onStartCommand method. this method is called when service is started and usually you do all stuff here. Remember that there are 3 options to return.
Edit 2:
try something like this
in on create
PendingIntent pi;
BroadcastReceiver br;
Intent myIntent;
#Override
public void onCreate()
{
super.onCreate();
myIntent = new Intent("something")
if(Build.Version.SDK_INT >= 16) //The flag we used here was only added at API 16
myIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
//use myIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); if you want to add more than one flag to this intent;
pi = PendingIntent.getBroadcast(context, 1, myIntent, 0);
br = new BroadcastReceiver ()
{
public void onReceive (Context context, Intent i) {
new thread(new Runnable()
{
public void run()
{
//do something
}
}).start();
}
};
And then in on start command
this.registerReceiver(br, new IntentFilter("something"));
My application starts when the device boots (Nexus 7). When my device boots if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) becomes true. Then the code inside the if crashes because the intent was not really a ACTION_NDEF_DISCOVERED but a boot.
I can put a try catch and then it won't crash. However then NFC won't work. To get NFC to work the application must be closed and reopened.
Is there a way to check the if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) but have it ignore the booting? It is really annoying because the if is checking for NFC not boot.
#Override
public void onResume()
{
super.onResume();
// NFC code.
Intent intent = getIntent();
String action = intent.getAction();
PendingIntent pi = this.createPendingResult(0x00A, new Intent(), 0);
nfcAdapter.enableForegroundDispatch(this, pi, null, null);
try
{
// NFC transfer. Receiving message here.
if(action != null && action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED))
{
Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage inNdefMessage = (NdefMessage) parcelables[0];
NdefRecord[] inNdefRecords = inNdefMessage.getRecords();
NdefRecord NdefRecord_0 = inNdefRecords[0];
String inMsg = new String(NdefRecord_0.getPayload());
Toast.makeText(getApplicationContext(), "Toasty: "+inMsg + action.toString(), Toast.LENGTH_LONG).show();
textInfo.setText(inMsg);
}
}
catch(Exception e)
{
Log.e("NFC", e.getMessage());
}
}
This is the code that checks for BOOTING.
public class BootManager extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent i = new Intent(context, Login_Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
In the Login_Activity, Could this be altering the intent?
#Override
protected void onNewIntent(Intent intent)
{
setIntent(intent);
}