这是myService类:
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;
}
}
这是WearActivity类:
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);
}
基本上,我想要做的是,一旦在后台服务上运行的加速度计达到某个阈值(9.8),那么我想在屏幕上显示一些东西用于磨损活动。我想要一个通知,说“你兴奋吗?"”,并希望它点击它时做点什么。首先,我不确定如何在此处添加通知,其次我不确定是否将其添加到我编写的showNotification()方法中的服务类中,或者只是说某种方式如果我通过门槛,转到主要活动并显示一个按钮? 更新:我已经添加了我的showNotification方法,但不确定它为什么不做任何事情。我为onSensorChanged()打印了一个日志语句,我看到它出现在LogCat中,所以它正在进入该方法。不知道如何让它显示此通知。