Android Service With Google Play Services Location Causing App to Open Randomly - android

I have a service which should capture the user's location and current battery level and send it to my firebase backend. For some reason it seems like whenever I have this service enabled in my app, my app will randomly open without any user interaction (even if the user is in another app, my app will pop open). Does anyone have any idea why this is happening?
Here is my code for the service:
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.server.converter.StringToIntConverter;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static android.R.attr.lines;
import static com.package.R.id.location;
import static com.package.R.id.view;
public class LocationBatteryService extends Service implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private String currentUser;
private String city;
private String country;
private float batteryLevel;
private int batteryLevelInt;
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
protected Location mCurrentLocation;
List<Address> addresses;
Geocoder geocoder;
#Override
public void onCreate() {
super.onCreate();
buildGoogleApiClient();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
protected synchronized void buildGoogleApiClient() {
Log.i("TAG", "Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
mGoogleApiClient.connect();
}
protected void createLocationRequest() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String locationSyncSettings = prefs.getString("location_sync_time", "");
long intervalTime = 5;
if(locationSyncSettings.equals("5 minutes")) {
intervalTime = 5;
}
if (locationSyncSettings.equals("10 minutes")) {
intervalTime = 10;
}
if (locationSyncSettings.equals("15 minutes")) {
intervalTime = 15;
}
if (locationSyncSettings.equals("30 minutes")) {
intervalTime = 30;
}
if (locationSyncSettings.equals("1 hour")) {
intervalTime = 60;
}
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(TimeUnit.MINUTES.toMillis(intervalTime));
mLocationRequest.setFastestInterval(60000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
#Override
public void onConnected(Bundle connectionHint) {
Log.i("TAG", "Connected to GoogleApiClient");
try {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
onLocationChanged(mCurrentLocation);
}
catch (SecurityException e){
Log.d("TAG", "Error: " + e);
}
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("TAG", "You got destoryed mate");
}
#Override
public void onLocationChanged(Location location) {
updateLocationBackend(mCurrentLocation);
}
#Override
public void onConnectionSuspended(int cause) {
// The connection to Google Play services was lost for some reason. We call connect() to
// attempt to re-establish the connection.
Log.i("TAG", "Connection suspended");
mGoogleApiClient.connect();
}
public void onTaskRemoved (Intent rootIntent){
this.stopSelf();
}
#Override
public void onConnectionFailed(ConnectionResult result) {
// Refer to the javadoc for ConnectionResult to see what error codes might be returned in
// onConnectionFailed.
Log.i("TAG", "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}
public float getBatteryLevel() {
Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
// Error checking that probably isn't needed but I added just in case.
if(level == -1 || scale == -1) {
return 50.0f;
}
return ((float)level / (float)scale) * 100.0f;
}
private void updateLocationBackend(final Location location) {
Log.i("TAG", "Location and battery being updated");
batteryLevel = getBatteryLevel();
batteryLevelInt = Math.round(batteryLevel);
// Get current user
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
currentUser = sharedPrefs.getString("Username", "");
// Get users added by you
DatabaseReference ref = FirebaseDatabase.getInstance().getReferenceFromUrl(Passwords.FB_URL).child("Relations").child(currentUser);
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user value
for (DataSnapshot ds : dataSnapshot.getChildren()) {
final String contactNumber = ds.getKey();
// Check to see if users added you
final DatabaseReference ref = FirebaseDatabase.getInstance().getReferenceFromUrl(Passwords.FB_URL).child("Contacts").child(contactNumber).child(currentUser);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.child("name").exists()) {
// User has not added you so do not update location
}
// User has added you so update location
else {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean contactLocationSetting = sharedPrefs.getBoolean(contactNumber + "_location_pref", true);
Log.d("TAG", "ContactLocationSetting for " + contactNumber + " is equal to: " + contactLocationSetting);
if (location != null && contactLocationSetting == true) {
Map updateLocation = new HashMap();
double latitude = location.getLatitude();
double longitude = location.getLongitude();
geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
} catch (IOException e) {
Log.e("TAG", "error is: " + e);
}
if (addresses.size() == 0) {
// Do nothing
} else {
city = addresses.get(0).getLocality();
country = addresses.get(0).getCountryName();
// String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
}
updateLocation.put("battery", batteryLevelInt);
updateLocation.put("latitude", latitude);
updateLocation.put("longitude", longitude);
updateLocation.put("city", city);
updateLocation.put("country", country);
updateLocation.put("lastUpdated", System.currentTimeMillis());
ref.updateChildren(updateLocation);
Log.d("TAG", "Updated location for: " + contactNumber);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TAG", "getUser:onCancelled", databaseError.toException());
}
});
}
}
Here is the code where I start the service:
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
public static ViewPager viewPager;
public static ViewPagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String currentUser = sharedPrefs.getString("Username", null);
if (currentUser == null) {
// Take user to log in screen
Log.d("TAG", "user needs to login");
Intent intent = new Intent(this, MyIntro.class);
startActivity(intent);
finish();
}
else {
// User already logged in
setupMainActivity();
}
}
#Override
public void onResume() {
super.onResume();
return;
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ChalkboardFragment(), getString(R.string.chalkboard_label));
adapter.addFragment(new ContactsFragment(), getString(R.string.contacts_label));
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ChalkboardFragment();
case 1:
return new ContactsFragment();
}
return null;
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
public void setupMainActivity() {
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean locationSetting = prefs.getBoolean("location_pref", false);
Log.d("TAG", "location_pref " + locationSetting);
if (isMyServiceRunning(LocationBatteryService.class) == false && locationSetting == true) {
startService(new Intent(this, LocationBatteryService.class));
}
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
Application Start:
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.digits.sdk.android.Digits;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterCore;
import io.fabric.sdk.android.Fabric;
public class ApplicationStart extends Application {
private static Context mContext;
#Override
public void onCreate() {
super.onCreate();
mContext = this.getApplicationContext();
/*if (!FirebaseApp.getApps(this).isEmpty()) {
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}*/
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String currentUser = sharedPrefs.getString("Username", null);
if (currentUser == null) {
// Take user to log in screen
Log.d("TAG", "user needs to login");
Intent intent = new Intent(this, MyIntro.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else {
// User already logged in
Log.d("TAG", "user logged in: " + currentUser);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY,TWITTER_SECRET);
Fabric.with(this, new TwitterCore(authConfig),new Digits.Builder().build());
}
public static Context getAppContext(){
return mContext;
}
}
Complete manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" android:protectionLevel="signature" />
<application android:name=".ApplicationStart" android:allowBackup="false" android:icon="#mipmap/ic_launcher"
android:label="#string/app_name" android:supportsRtl="true" android:theme="#style/AppTheme">
<activity android:name=".MainActivity" android:label="#string/app_name" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity" android:label="#string/title_activity_login"
android:parentActivityName=".MainActivity" />
<activity android:name=".ContactsSettingsActivity" android:label="Contact Settings"
android:launchMode="singleTop">
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.package.ContactsChalkboard" />
</activity>
<activity android:name=".MyIntro" />
<activity android:name=".ContactsChalkboard" />
<activity android:name=".AlarmActivity" />
<activity android:name=".AddContactActivity" android:parentActivityName=".MainActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.package.MainActivity" />
</activity>
<activity android:name=".PictureNameActivity" android:label="#string/title_activity_picture_name"
android:parentActivityName=".LoginActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.package.LoginActivity" />
</activity>
<activity android:name=".Test" />
<activity android:name=".SettingsActivity" android:label="Settings">
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.package.MainActivity" />
</activity>
<activity android:name="com.package.GeofenceActivity" android:label="#string/title_activity_geofence">
</activity>
<service android:name=".LocationBatteryService" android:enabled="true" android:exported="true" />
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".GeofenceTransitionsIntentService" />
<receiver android:name=".AlarmReceiver" android:enabled="true" android:exported="true" />
<receiver android:name=".BootReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<meta-data android:name="com.google.android.geo.API_KEY" android:value="API KEY VALUE" />
<meta-data android:name="io.fabric.ApiKey" android:value="API KEY VALUE" />
</application>
</manifest>

Well, there is no guarantee that the system keeps running your service all the time. When the application is killed, and a new location has to be delivered the play services lib will instantiate the application and start your service. You have to remove startActivity from your application class.

Related

bluetooth scan not working android studio api 29

I have an app made with android studio that connects to my propitiatory wind gauge that I developed. everything was working perfectly until I submitted it to google. They rejected it saying i need targetSdkVersion 29. when I build it with targetSdkVersion 29 the app no longer scans. But it works when I build it with targetSdkVersion 26
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.windgauge"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.windgauge.ScanActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.windgauge.DeviceActivity" android:label="#string/app_name" />
</application>
</manifest>
ScanActivity.java
package com.windgauge;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.windgauge.util.BleUtil;
import com.windgauge.util.ScannedDevice;
import java.util.ArrayList;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;
#RuntimePermissions
public class ScanActivity extends AppCompatActivity implements BluetoothAdapter.LeScanCallback {
private BluetoothAdapter mBTAdapter;
private DeviceAdapter mDeviceAdapter;
private boolean mIsScanning;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_scan);
Button scan = findViewById(R.id.scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ScanActivityPermissionsDispatcher.startScanWithPermissionCheck(ScanActivity.this);
}
});
init();
}
#Override
protected void onDestroy() {
super.onDestroy();
stopScan();
}
#Override
public void onLeScan(final BluetoothDevice newDeivce, final int newRssi,
final byte[] newScanRecord) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mDeviceAdapter.update(newDeivce, newRssi, newScanRecord);
}
});
}
private void init() {
// BLE check
if (!BleUtil.isBLESupported(this)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
return;
}
// BT check
BluetoothManager manager = BleUtil.getManager(this);
if (manager != null) {
mBTAdapter = manager.getAdapter();
}
if (mBTAdapter == null) {
Toast.makeText(this, R.string.bt_unavailable, Toast.LENGTH_SHORT).show();
finish();
return;
}
if (!mBTAdapter.isEnabled()) {
Toast.makeText(this, R.string.bt_disabled, Toast.LENGTH_SHORT).show();
finish();
return;
}
// init listview
ListView deviceListView = (ListView) findViewById(R.id.list);
mDeviceAdapter = new DeviceAdapter(this, R.layout.listitem_device,
new ArrayList<ScannedDevice>());
deviceListView.setAdapter(mDeviceAdapter);
deviceListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) {
ScannedDevice item = mDeviceAdapter.getItem(position);
if (item != null) {
Intent intent = new Intent(view.getContext(), DeviceActivity.class);
BluetoothDevice selectedDevice = item.getDevice();
intent.putExtra(DeviceActivity.EXTRA_BLUETOOTH_DEVICE, selectedDevice);
startActivity(intent);
// stop before change Activity
stopScan();
}
}
});
stopScan();
}
#NeedsPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
void startScan() {
if ((mBTAdapter != null) && (!mIsScanning)) {
mBTAdapter.startLeScan(this);
mIsScanning = true;
setProgressBarIndeterminateVisibility(true);
invalidateOptionsMenu();
}
}
private void stopScan() {
if (mBTAdapter != null) {
mBTAdapter.stopLeScan(this);
}
mIsScanning = false;
setProgressBarIndeterminateVisibility(false);
invalidateOptionsMenu();
}
}

Sending notifications from smartphone to Wear OS smartwatch

I've been trying to develop a simple app that can send notifications from a smartphone to an Wear OS smartwatch (more specifically, a Huawei Smartwatch 2).
I've used this code as a basis: github link. Note that this code doens't have the "notifications part" I'm trying to develop.
The bottom line is: I can receive data on the smartwatch, but only when the app on it is running. The ServiceListener service, from what I could tell, from debugging, is not being activated. Basically, anything I write on that file, even Log data, isn't being displayed.
The ServiceListener service:
package com.example.nowor_000.sincronizaciondatos;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;
import static android.content.ContentValues.TAG;
import static com.google.android.gms.wearable.PutDataRequest.WEAR_URI_SCHEME;
/**
* Created by nowor_000 on 26/11/2015.
*/
public class ServiceListener extends WearableListenerService {
public static final String NOTIFICATION_PATH = "/notification";
public static final String NOTIFICATION_TIMESTAMP = "timestamp";
public static final String NOTIFICATION_TITLE = "title";
public static final String NOTIFICATION_CONTENT = "content";
public static final String ACTION_DISMISS = "com.example.nowor_000.sincronizaciondatos.DISMISS";
//private static final String WEAR_INICIAR_ACTIVIDAD = "/iniciar";
private static final String START_ACTIVITY_PATH = "/start-activity";
private int notificationId = 001;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand ServiceListener.");
if (null != intent) {
String action = intent.getAction();
if (ACTION_DISMISS.equals(action)) {
dismissNotification();
}
}
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDataChanged(DataEventBuffer dataEvents) {
Log.i(TAG, "onDataChanged ServiceListener.");
for (DataEvent dataEvent : dataEvents) {
if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
if (NOTIFICATION_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(dataEvent.getDataItem());
String title = dataMapItem.getDataMap().getString(NOTIFICATION_TITLE);
String content = dataMapItem.getDataMap().getString(NOTIFICATION_CONTENT);
sendNotification(title, content);
}
}
}
}
private void sendNotification(String title, String content) {
Log.i(TAG, "Sending notification.");
// this intent will open the activity when the user taps the "open" action on the notification
Intent viewIntent = new Intent(this, MainActivity.class);
PendingIntent pendingViewIntent = PendingIntent.getActivity(this, 0, viewIntent, 0);
// this intent will be sent when the user swipes the notification to dismiss it
Intent dismissIntent = new Intent(ACTION_DISMISS);
PendingIntent pendingDeleteIntent = PendingIntent.getService(this, 0, dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(content)
.setDeleteIntent(pendingDeleteIntent)
.setContentIntent(pendingViewIntent);
Notification notification = builder.build();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(notificationId++, notification);
}
private void dismissNotification() {
new ServiceListener.DismissNotificationCommand(this).execute();
}
private class DismissNotificationCommand implements GoogleApiClient.ConnectionCallbacks, ResultCallback<DataApi.DeleteDataItemsResult>, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "DismissNotification";
private final GoogleApiClient mGoogleApiClient;
public DismissNotificationCommand(Context context) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
public void execute() {
mGoogleApiClient.connect();
}
#Override
public void onConnected(Bundle bundle) {
final Uri dataItemUri =
new Uri.Builder().scheme(WEAR_URI_SCHEME).path(NOTIFICATION_PATH).build();
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Deleting Uri: " + dataItemUri.toString());
}
Wearable.DataApi.deleteDataItems(
mGoogleApiClient, dataItemUri).setResultCallback(this);
}
#Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "onConnectionSuspended");
}
#Override
public void onResult(DataApi.DeleteDataItemsResult deleteDataItemsResult) {
if (!deleteDataItemsResult.getStatus().isSuccess()) {
Log.e(TAG, "dismissWearableNotification(): failed to delete DataItem");
}
mGoogleApiClient.disconnect();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed");
}
}
}
The MainActivity on the smartphone:
package com.example.nowor_000.sincronizaciondatos;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;
import static android.content.ContentValues.TAG;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
public static final String NOTIFICATION_PATH = "/notification";
public static final String NOTIFICATION_TIMESTAMP = "timestamp";
public static final String NOTIFICATION_TITLE = "title";
public static final String NOTIFICATION_CONTENT = "content";
public static final String ACTION_DISMISS = "de.peterfriese.notificationwithopenactivityonwearableaction.DISMISS";
/**
* sincronizacion
*/
private static final String KEY_CONTADOR = "com.example.key.contador";
private static final String ITEM_CONTADOR = "/contador";
/**
* sincronizacion
*/
private static final String WEAR_INICIAR_ACTIVIDAD = "/iniciar";
private static final String WEAR_ENVIAR_TEXTO = "/enviar_texto";
Button btnEnviarTexto;
EditText editText;
private int contador;
/**
* CLIENTE
*/
private GoogleApiClient apiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
/**
*API CLIENT es clave para establecer la comunicaciĆ³n
*/
apiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
addViews();
/**
* sincronizacion
*/
final TextView textoContador = (TextView) findViewById(R.id.textoContador);
textoContador.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
contador++;
textoContador.setText(Integer.toString(contador));
PutDataMapRequest putDataMapReq = PutDataMapRequest.create(ITEM_CONTADOR);
putDataMapReq.getDataMap().putInt(KEY_CONTADOR, contador);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
PendingResult<DataApi.DataItemResult> resultado = Wearable.DataApi.putDataItem(apiClient, putDataReq);
/**
* ESTA LINEA SE ME OLVIDO EN EL VIDEOTUTORIAL
*/
enviarMensaje(ITEM_CONTADOR, Integer.toString(contador));
}
});
/**
* sincronizacion
*/
}
private void addViews() {
editText = (EditText) findViewById(R.id.edtitText);
btnEnviarTexto = (Button) findViewById(R.id.btnEnviarTexto);
btnEnviarTexto.setOnClickListener(this);
}
#Override
public void onClick(View v) {
sendNotification();
switch (v.getId()) {
case R.id.btnEnviarTexto:
String text = editText.getText().toString();
if (!TextUtils.isEmpty(text)) {
enviarMensaje(WEAR_ENVIAR_TEXTO, text);
editText.getText().clear();
editText.requestFocus();
} else {
enviarMensaje(WEAR_ENVIAR_TEXTO, "\n->Escribe un mensaje, en el EditText<-");
}
break;
}
}
//<editor-fold desc="ENVIAR MENSAJE EditText">
private void enviarMensaje(final String path, final String texto) {
new Thread(new Runnable() {
#Override
public void run() {
NodeApi.GetConnectedNodesResult nodos = Wearable.NodeApi.getConnectedNodes(apiClient).await();
for (Node nodo : nodos.getNodes()) {
Wearable.MessageApi.sendMessage(apiClient, nodo.getId(), path, texto.getBytes())
.setResultCallback(
new ResultCallback<MessageApi.SendMessageResult>() {
#Override
public void onResult(MessageApi.SendMessageResult resultado) {
if (!resultado.getStatus().isSuccess()) {
Log.e("sincronizacion", "Error al enviar mensaje. Codigo" + resultado.getStatus().getStatusCode());
}
}
}
);
}
}
}).start();
}
//</editor-fold>
//<editor-fold desc="CICLO DE VIDA">
#Override
protected void onStart() {
super.onStart();
apiClient.connect();
}
#Override
protected void onStop() {
if (apiClient != null && apiClient.isConnected()) {
apiClient.disconnect();
}
super.onStop();
}
//</editor-fold>
//<editor-fold desc="METODOS API DATA LAYER">
#Override
public void onConnected(Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
//</editor-fold>
//<editor-fold desc="MENU">
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//</editor-fold>
private void sendNotification() {
if (apiClient.isConnected()) {
Log.i(TAG, "Sending data.");
PutDataMapRequest dataMapRequest = PutDataMapRequest.create(NOTIFICATION_PATH);
// Make sure the data item is unique. Usually, this will not be required, as the payload
// (in this case the title and the content of the notification) will be different for almost all
// situations. However, in this example, the text and the content are always the same, so we need
// to disambiguate the data item by adding a field that contains teh current time in milliseconds.
dataMapRequest.getDataMap().putDouble(NOTIFICATION_TIMESTAMP, System.currentTimeMillis());
dataMapRequest.getDataMap().putString(NOTIFICATION_TITLE, "This is the title");
dataMapRequest.getDataMap().putString(NOTIFICATION_CONTENT, "This is a notification with some text.");
PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest().setUrgent();
Wearable.DataApi.putDataItem(apiClient, putDataRequest);
} else {
Log.e(TAG, "No connection to wearable available!");
}
}
}
The AndroidManifest for the smartphone:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nowor_000.sincronizaciondatos">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<!--ETIQUETA PPARA INDICAR LA VERSION DE GOOGLE PLAY QUE ESTAMOS USANDO -->
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The AndroidManifest for the smartwatch:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nowor_000.sincronizaciondatos">
<uses-feature android:name="android.hardware.type.watch" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#android:style/Theme.DeviceDefault">
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<uses-library android:name="com.google.android.wearable"
android:required="true" />
<activity
android:name=".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=".ServiceListener"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data android:scheme="wear" android:host="*"
android:path="/start-activity" />
</intent-filter>
</service>
</application>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
</manifest>
If you need any more code please say. Thanks!

Android Geofece Exit/Enter in geofence Events not triggerring

I am stuk with this issue for more than a week.Goefence events not triggering on entering or existing the Geofence. i have tested it several times on emulater.
Here is my MainActivity
**
package com.example.internet.ytgeofence;
import android.Manifest;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.Api;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";
GoogleApiClient googleApiClient = null;
protected ArrayList<Geofence> mGeofenceList;
private Context mContext ;
Button startLocationMonitoring, startGeofenceMonitoring, stopGeofenceMonitoring;
TextView t;
public List<Geofence> listGeofence;
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*int resultCode =
GooglePlayServicesUtil.
isGooglePlayServicesAvailable(this);*/
mGeofenceList = new ArrayList<Geofence>();
startLocationMonitoring = (Button) findViewById(R.id.button2);
startGeofenceMonitoring = (Button) findViewById(R.id.button3);
stopGeofenceMonitoring = (Button) findViewById(R.id.button4);
t = (TextView) findViewById(R.id.textView2);
mContext=getApplicationContext();
startLocationMonitoring.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StartLocationMonitoring();
}
});
startGeofenceMonitoring.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StartGeofenceMonitoring();
}
});
stopGeofenceMonitoring.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StopGeofenceMonitoring();
}
});
googleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
#Override
public void onConnected(#Nullable Bundle bundle) {
Log.d(TAG, "Connected to google Api Client");
}
#Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "suspended connection to google Api Client");
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Log.d(TAG, "Failed to connect to google api client" + connectionResult.getErrorMessage());
}
}).build();
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1234);
googleApiClient.connect();
}
#Override
protected void onStart() {
Log.d(TAG, "On start Called");
Toast.makeText(this,"On start called" ,Toast.LENGTH_LONG).show();
super.onStart();
googleApiClient.reconnect();
}
#Override
protected void onStop() {
Log.d(TAG, "On stop Called");
super.onStop();
googleApiClient.disconnect();
}
#Override
protected void onResume() {
Log.d(TAG, "On Resume Called");
super.onResume();
int response = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if (response != ConnectionResult.SUCCESS) {
Log.d(TAG, "Google Paly Services not avaliable.show dialouge to download");
GoogleApiAvailability.getInstance().getErrorDialog(this, response, 1).show();
} else {
Log.d(TAG, "Google Paly Services avaliable");
}
}
private void StartLocationMonitoring() {
Log.d(TAG, "Location Monitoring Start");
try {
LocationRequest locationRequest = LocationRequest.create()
.setInterval(10000)
.setFastestInterval(5000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
t = (TextView) findViewById(R.id.textView2);
t.setText("Lat:" + location.getLatitude() + "Lng" + location.getLongitude());
Log.d(TAG, "Location Lat/Lng" + location.getLongitude() + " " + location.getLongitude());
}
});
} catch (Exception ex) {
Log.d(TAG, "Exception in location Monitoring" + ex.toString());
}
}
private void StartGeofenceMonitoring() {
Geofence geofence = new Geofence.Builder().setRequestId("SAN Loaction")
.setCircularRegion(48.848016, 2.346888, 200)
.setExpirationDuration(Geofence.NEVER_EXPIRE).setNotificationResponsiveness(1000)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT).build();
mGeofenceList.add(geofence);
GeofencingRequest geofencingRequest = getGeofencingRequest();
Intent intent = new Intent(MainActivity.this, GeofenceService.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
if (!googleApiClient.isConnected()) {
Log.d(TAG, "Google API Client Not Connected");
Toast.makeText(getApplicationContext()
,"Google api client not connected",Toast.LENGTH_LONG);
} else {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.GeofencingApi.addGeofences(googleApiClient, geofencingRequest, pendingIntent).setResultCallback(new ResultCallback<Status>() {
#Override
public void onResult(#NonNull Status status) {
if (status.isSuccess())
{
Log.d(TAG,"Geofence added Successfully");
Toast.makeText(getApplicationContext(),"Geo fence added successfully",Toast.LENGTH_LONG).show();
}
else
{
Log.d(TAG,"Failed to add Geofence"+status.getStatus());
Toast.makeText(getApplicationContext(),"Geofence not added successfully",Toast.LENGTH_LONG);
}
}
});
}
}catch (SecurityException ex)
{
Log.d(TAG," Security Exception in api client");
Toast.makeText(getApplicationContext(),"Geofence not added successfully"+ex.toString(),Toast.LENGTH_LONG);
}
}
private void StopGeofenceMonitoring(){
Log.d(TAG,"Stop geofence called");
ArrayList<String> geofenceids=new ArrayList<String>();
geofenceids.add("SAN Loaction");
LocationServices.GeofencingApi.removeGeofences(googleApiClient,geofenceids);
}
private GeofencingRequest getGeofencingRequest() {
Log.d(TAG,"Inside Getgeofencing request");
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(mGeofenceList);
return builder.build();
}
}
The Activity_main.xaml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.internet.ytgeofence.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/textView" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_marginLeft="59dp"
android:layout_marginStart="59dp"
android:layout_marginTop="75dp"
android:layout_toEndOf="#+id/textView"
android:layout_toRightOf="#+id/textView"
android:text="Start Location Monitoring" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2"
android:layout_below="#+id/button2"
android:layout_marginTop="22dp"
android:text="Start Geofence Monitoring" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3"
android:layout_centerVertical="true"
android:text="Stop Geofence Monitoring" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="104dp"
android:text="Status" />
The GeofenceService is
package com.example.internet.ytgeofence;
import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;
import java.util.List;
public class GeofenceService extends IntentService {
public static final String TAG="GeofenceService";
public GeofenceService() {
super(TAG);
Toast.makeText(this,"Inside Service",Toast.LENGTH_LONG).show();
Log.d(TAG,"Inside Service Constructer");
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
Log.d(TAG,"Inside IntentHandler");
GeofencingEvent event=GeofencingEvent.fromIntent(intent);
if(event.hasError())
{
}
else
{
int transition=event.getGeofenceTransition();
List<Geofence> geofences=event.getTriggeringGeofences();
Geofence geofence=geofences.get(0);
String RequestID=geofence.getRequestId();
if(transition==Geofence.GEOFENCE_TRANSITION_ENTER)
{
Log.d(TAG,"Entering Geofence Area"+RequestID);
Toast.makeText(this,"Entering Geofence Area",Toast.LENGTH_LONG);
}
else if(transition==Geofence.GEOFENCE_TRANSITION_ENTER)
{
Log.d(TAG,"Exiting Geofence Area"+RequestID);
Toast.makeText(this,"Exiting Geofence Area",Toast.LENGTH_LONG);
}
}
}
}
And the Manifest.xaml is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.internet.ytgeofence">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".GeofenceService"/>
</application>
Any Help is Highly Apperciated.
Your code is fine just use real device, and enable GPS, also give app location permission, that set, I use emulater before it's not worked with me.
Update
I try your code, there is some point to fix this issue, first add this code in manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<!-- add this -->
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity android:name=".MainActivity">
in your service remove Toast and use log just because it's crashing app, this is the result
I add GeofencingRequest.INITIAL_TRIGGER_EXIT for GeofencingRequest just to ensure it will work perfectly
private GeofencingRequest getGeofencingRequest() {
Log.d(TAG,"Inside Getgeofencing request");
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_EXIT);
builder.addGeofences(mGeofenceList);
return builder.build();
}
in the manifest I add this permission -I think you already add it, put just in case- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

android ActivityRecognition not calling onHandleIntent

I have put together this piece of code to get the users activity, to see is he walking or driving or still, but its not working, onHandleIntent never called. it is connecting to GoogleApiClient.
Here is my code
Activity Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/msg" />
MainActivity
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.ActivityRecognition;
public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private Context mContext;
private GoogleApiClient mGApiClient;
private BroadcastReceiver receiver;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the textview
textView = (TextView) findViewById(R.id.msg);
//Set the context
mContext = this;
//Check Google Play Service Available
if(isPlayServiceAvailable()) {
mGApiClient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
//Connect to gPlay
mGApiClient.connect();
}else{
Toast.makeText(mContext, "Google Play Service not Available", Toast.LENGTH_LONG).show();
}
//Broadcast receiver
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String v = "Activity :" +
intent.getStringExtra("act") + " " +
"Confidence : " + intent.getExtras().getInt("confidence") + "n";
v += textView.getText();
textView.setText(v);
}
};
IntentFilter filter = new IntentFilter();
filter.addAction("SAVVY");
registerReceiver(receiver, filter);
}
private boolean isPlayServiceAvailable() {
return GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS;
}
#Override
public void onConnected(Bundle bundle) {
Intent i = new Intent(this, ActivityRecognitionIntentService.class);
PendingIntent mActivityRecongPendingIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("Saquib", "connected to ActRecog " + "PI " +mActivityRecongPendingIntent.toString() );
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGApiClient, 0, mActivityRecongPendingIntent);
}
#Override
public void onConnectionSuspended(int i) {
Log.d("Saquib", "suspended to ActRecog");
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("Saquib", "Not connected to ActRecog");
}
#Override
protected void onDestroy() {
super.onDestroy();
mGApiClient.disconnect();
unregisterReceiver(receiver);
}
}
ActivityRecognitionIntentService
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
/**
* Created by tutsberry on 17/03/15.
*/
public class ActivityRecognitionIntentService extends IntentService {
public ActivityRecognitionIntentService() {
super("ActivityRecognitionIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
if(ActivityRecognitionResult.hasResult(intent)) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
DetectedActivity detectedActivity = result.getMostProbableActivity();
int confidence = detectedActivity.getConfidence();
String mostProbableName = getActivityName(detectedActivity.getType());
Intent i = new Intent("SAVVY");
i.putExtra("act", mostProbableName);
i.putExtra("confidence", confidence);
Log.d("Saquib", "mostProbableName " + mostProbableName);
Log.d("Saquib", "Confidence : " + confidence);
//Send Broadcast
this.sendBroadcast(i);
}else {
Log.d("Saquib", "Intent had no data returned");
}
}
private String getActivityName(int type) {
switch (type)
{
case DetectedActivity.IN_VEHICLE:
return "in_vehicle";
case DetectedActivity.ON_BICYCLE:
return "on_bicycle";
case DetectedActivity.WALKING:
return "walking";
case DetectedActivity.STILL:
return "still";
case DetectedActivity.TILTING:
return "tilting";
case DetectedActivity.UNKNOWN:
return "unknown";
case DetectedActivity.RUNNING:
return "running";
}
return "n/a";
}
}
and manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutsberry.moveyours" >
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".ActivityRecognitionIntentService">
</service>
</application>
</manifest>
Guys please help, google doc is no use for ActivityRecognition
Maybe you are missing the
<uses-permission android:name="android.permission.INTERNET" />
Make sure that you are connected to the internet beforehand. I think otherwise it will not work. The first time you connect to google play services you must be connected to the internet.

Does Android ActivityRecognitionApi work , No Updates received on onHandleIntent

Guys have anybody got the ActivityrecognitionAPI to work in Android. It does not give any updates. Have tried the API documents, Plenty of Examples. OnHandleIntent does not fire.
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
public class ActivityRecognitionIntentService extends IntentService{
ActivityRecognitionResult result;
Intent i;
DetectedActivity mpactivity;
public ActivityRecognitionIntentService() {
super("ActivityRecognitionIntentService");
i = new Intent("ACTIVITY_RECOGNITION_DATA");
}
private String getTypes(int type) {
if(type == DetectedActivity.UNKNOWN)
return "Unknown";
else if(type == DetectedActivity.IN_VEHICLE)
return "In Vehicle";
else if(type == DetectedActivity.ON_BICYCLE)
return "On Bicycle";
else if(type == DetectedActivity.RUNNING)
return "Running";
else if(type == DetectedActivity.ON_FOOT)
return "On Foot";
else if(type == DetectedActivity.STILL)
return "Still";
else if(type == DetectedActivity.TILTING)
return "Tilting";
else if(type == DetectedActivity.WALKING)
return "Walking";
else
return "";
}
#Override
protected void onHandleIntent(Intent intent) {
if (intent.getAction() == "ActivityRecognitionIntentService") {
if(ActivityRecognitionResult.hasResult(intent)){
result = ActivityRecognitionResult.extractResult(intent);
mpactivity = result.getMostProbableActivity();
i.putExtra("Activity", getTypes(mpactivity.getType()));
i.putExtra("Confidence", mpactivity.getConfidence());
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}
}
}
The Main activity is as
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.ActivityRecognition;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.TextView;
import android.widget.Toast;
public class Actrecogex extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
TextView textView1;
GoogleApiClient mGoogleActclient;
PendingIntent mActivityRecognitionPendingIntent;
Intent i;
LocalBroadcastManager mBroadcastManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actrecogex);
textView1 = new TextView(this);
textView1 =(TextView)findViewById(R.id.textView1);
i = new Intent(this, ActivityRecognitionIntentService.class);
mBroadcastManager = LocalBroadcastManager.getInstance(this);
mGoogleActclient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleActclient.connect();
}
#Override
public void onConnectionFailed(ConnectionResult arg0) {
textView1.setText("Failed Connection" + arg0);
}
#Override
public void onConnected(Bundle arg0) {
i.setAction("ActivityRecognitionIntentService");
mActivityRecognitionPendingIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleActclient, 0, mActivityRecognitionPendingIntent);
}
#Override
public void onConnectionSuspended(int arg0) {
textView1.setText("Failed Suspended" + arg0);
}
private BroadcastReceiver receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "Service", Toast.LENGTH_SHORT).show();
String v = "Activity :" + intent.getStringExtra("Activity") + " " + "Confidence : " + intent.getExtras().getInt("Confidence") + "\n";
v += textView1.getText() ;
textView1.setText(v);
}
};
#Override
public void onDisconnected() {
textView1.setText("Disconnected" );
}
}
Manifest
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Actrecogex"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version"/>
<service android:name="ActivityRecognitionIntentService" android:exported="false"></service>
</application>
Does the code really work, Or I am wasting time. Seriously want to go back Activityrecogntionclient. Testing the App on Gingerbread and Kitkat. Both does not budge
It seems your definition of the service in the Manifest is wrong: it is missing a leading period. It should be
<service android:name=".ActivityRecognitionIntentService" android:exported="false"></service>
Your onHandleIntent() uses
intent.getAction() == "ActivityRecognitionIntentService"
But you cannot compare strings with == so your if statement never returns true. Instead, you must use equals() or the equivalent TextUtils.equals() (which also handles cases where either argument is null):
"ActivityRecognitionIntentService".equals(intent.getAction())
// OR
TextUtils.equals(intent.getAction(), "ActivityRecognitionIntentService")
if (intent.getAction() == "ActivityRecognitionIntentService") {
}
Was giving null value. So the periodic Error Message.
The Main Problem was the Broadcastreceiver was not receiving anything. So needed to have separate class for Broadcastreceiver. It is Working now.
<receiver android:name="ActBreceiver" android:exported="false">
<intent-filter>
<action android:name="ACTIVITY_RECOGNITION_DATA"/>
</intent-filter>
</receiver>

Categories

Resources