1

This is the myService class:

package me.smarthwatches.simplenotification;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service implements SensorEventListener {
    private static final String DEBUG_TAG = "AccelLoggerService";
    private SensorManager mSensorManager;
    private Sensor mSensor;

// on start command: register listener, on create:, and on destroy, Ibinder return null
    //get the system service

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
//        return super.onStartCommand(intent, flags, startId);
        return START_STICKY; // want service to continue running until its explicitly stopped so return sticky
    }

    @Override
    public void onCreate() {
//        super.onCreate();
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onDestroy() {
//        super.onDestroy();
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
        return;
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (Math.abs(event.values[0]) > 9.8 || Math.abs(event.values[1]) > 9.8 || Math.abs(event.values[2]) > 9.8) {
            Log.v(MyService.DEBUG_TAG, "value is greater than 9");
            //need to launch a notification? ask if person is excited?
            showNotification();
        }
        mSensorManager.unregisterListener(this);
    }


/** Notification to show person is excited */
private void showNotification() {
    // create a button here asking if excited
    Notification notification = new NotificationCompat.Builder(getApplication())
            .setContentTitle("Excited")
            .setContentText("Well are you?")
            .extend(
                    new NotificationCompat.WearableExtender().setHintShowBackgroundOnly(true))
            .build();

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplication());

    int notificationId = 2;
    notificationManager.notify(notificationId, notification);

}


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        return null;
    }
}

This is the WearActivity Class:

package me.smarthwatches.simplenotification;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.widget.TextView;

public class WearActivity extends Activity {
private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wear);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
        }
    });

    Intent serviceIntent = new Intent(getApplicationContext(), MyService.class);
    startService(serviceIntent);


}

Essentially, what I'd like to do is that once the accelerometer, which is running on a service in the background, reaches a certain threshold (9.8), then I want to display something to the screen for the Wear Activity. I'd like a notification that says "Are you excited?", and want it to do something when I click on it. First, I'm not sure how to even add the notification here, and second I'm not sure if I add it to the service class in showNotification() method that I made up, or just say somehow if I pass the threshold, go to the mainactivity and display a button? UPDATE: I've added my showNotification method, but not sure why it doesn't do anything. I printed a log statement for onSensorChanged() and I see it showing up in the LogCat so it is entering that method. Not sure how to make it show this notification though.

Ace Haidrey
  • 928
  • 1
  • 10
  • 26

0 Answers0