[英]How to receive a notification every hour if a condition is met on android studio?
[英]How to send notification with a delay when a condition is met in Android Studio
我正在創建一個應用程序,我希望我在一定時間清醒時發送通知。
我通過計算手機的加速度並將其添加到總數中來做到這一點。 這應該是激活計時器的某種閾值。
當按下按鈕時,我能夠發送通知。
但是現在我想在“accelerationTotalValue > 1000”時發送這個通知。
因為這是我在 Android Studio 和使用 java 的第一個項目,所以我對這一切都很陌生。
我的 java 活動
public class calibration extends AppCompatActivity {
TextView txt_currentAccel, txt_prevAccel, txt_acceleration;
TextView txt_totalAccel;
ProgressBar prog_shakeMeter;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private double accelerationPreviousValue;
private double accelerationTotalValue;
Button notifyBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calibration_layout);
// get layout ID's acceleration
txt_acceleration = findViewById(R.id.txt_accel);
txt_prevAccel = findViewById(R.id.txt_prevAccel);
txt_currentAccel = findViewById(R.id.txt_currentAccel);
txt_totalAccel = findViewById(R.id.txt_totalAccel);
prog_shakeMeter = findViewById(R.id.prog_shakeMeter);
// get info sensors
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// notification
// open main page when clicking on notification
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
// find button to activate notification
notifyBtn = findViewById(R.id.Notification_Button);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("My Notification","My Notification", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
notifyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(calibration.this,"My Notification");
builder.setContentTitle("My Title");
builder.setContentText("This is a simple notification");
builder.setSmallIcon(R.mipmap.ic_launcher_foreground);
builder.setAutoCancel(true);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setContentIntent(resultPendingIntent);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(calibration.this);
managerCompat.notify(1,builder.build());
}
});
}
// calculation acceleration
private final SensorEventListener sensorEventListener = new SensorEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
double accelerationCurrentValue = Math.sqrt((x * x + y * y + z * z));
double changeIntAcceleration = Math.abs(accelerationCurrentValue - accelerationPreviousValue);
accelerationPreviousValue = accelerationCurrentValue;
accelerationTotalValue = accelerationTotalValue + changeIntAcceleration;
txt_currentAccel.setText("Current = "+ accelerationCurrentValue);
txt_prevAccel.setText("Prev = "+ accelerationCurrentValue);
txt_acceleration.setText("Acceleration change = "+ changeIntAcceleration);
txt_totalAccel.setText("Total = "+ accelerationTotalValue);
prog_shakeMeter.setProgress((int)changeIntAcceleration);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
protected void onResume() {
super.onResume();
mSensorManager.registerListener(sensorEventListener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(sensorEventListener);
}
}
您可以將 notifyBtn onClick
中的所有內容放到單獨的 function 中,我們稱之為notify
,然后在代碼中添加一行:
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
// Your previous code
accelerationTotalValue = accelerationTotalValue + changeIntAcceleration;
if (accelerationTotalValue > 1000) {
notify();
}
// Rest of your code
}
請記住,您當前的代碼只會在您的應用程序處於前台時運行。 我建議在這里閱讀 Android 的 Activity Lifecycle。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.