Android 如何知道在用户不通知时发出通知';t执行应用程序
我想做一个通知,当用户退出android应用程序时开始计时。若用户在1小时后并没有执行应用程序,它会通知我执行,若用户忽略它,它会执行保存的SMS消息。我在计时器上找到了一些示例,但我不知道如何找到应用程序退出时间。请给我一些完整代码的建议。我非常需要它Android 如何知道在用户不通知时发出通知';t执行应用程序,android,timer,notifications,sms,Android,Timer,Notifications,Sms,我想做一个通知,当用户退出android应用程序时开始计时。若用户在1小时后并没有执行应用程序,它会通知我执行,若用户忽略它,它会执行保存的SMS消息。我在计时器上找到了一些示例,但我不知道如何找到应用程序退出时间。请给我一些完整代码的建议。我非常需要它 TimerTask task = new TimerTask(){ public void run() { try { mainTime++; int min = main
TimerTask task = new TimerTask(){
public void run() {
try {
mainTime++;
int min = mainTime / 60;
int sec = mainTime % 60;
String strTime = String.format("%s : %s", min, sec);
} catch (Exception e) {
e.printStackTrace();
}
}
};
Timer mTimer = new Timer();
mTimer.schedule(task, 0, 60000);
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("Chack your app", smsBody);
sendIntent.putExtra("12345678", phonenumber);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
好的,当应用程序退出时,您需要做的是在本地存储系统时间(可能使用
SharedReferences
)。您可以注册一个BroadcastReceiver
,当应用程序退出时,当本地存储的时间超过1小时或某个时间时,它将帮助您触发一些操作
如果您想知道如何以编程方式处理何时以及如何退出应用程序,请参阅。您也可以尝试使用Android报警系统。一旦用户退出应用程序,您就可以设置警报。比如: YourActivityOrFragment.java
@Override
protected void onStop() {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.add(Calendar.HOUR,1);
scheduleAlarm(c.getTimeInMillis());
}
private void scheduleAlarm(long time) {
Intent yourIntent = new Intent("Some_ID");
PendingIntent pi = PendingIntent.getBroadcast(YourClass.this, ALARM_ID, yourIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Put some extras here, if you need so. Like:
// yourIntent.putExtra("field","value");
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,time,pi);
}
public class AlarmReceiver extends BroadcastReceiver {
private static final String LOG_TAG = AlarmReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "Alarm fired!");
Intent it = new Intent(context, YourNotificationHandler.class);
// Get your Extras here. And do whatever you want, if you need.
// For what you said, there's no need to start an Activity, so let's handle that alarm as a service.
context.startService(it);
// But if for some reason you want to start an Activity, just do it like:
// context.startActivity(it);
}
现在,创建一个BroadcastReceiver
来处理这些警报
AlarmReceiver.java
@Override
protected void onStop() {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.add(Calendar.HOUR,1);
scheduleAlarm(c.getTimeInMillis());
}
private void scheduleAlarm(long time) {
Intent yourIntent = new Intent("Some_ID");
PendingIntent pi = PendingIntent.getBroadcast(YourClass.this, ALARM_ID, yourIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Put some extras here, if you need so. Like:
// yourIntent.putExtra("field","value");
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,time,pi);
}
public class AlarmReceiver extends BroadcastReceiver {
private static final String LOG_TAG = AlarmReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "Alarm fired!");
Intent it = new Intent(context, YourNotificationHandler.class);
// Get your Extras here. And do whatever you want, if you need.
// For what you said, there's no need to start an Activity, so let's handle that alarm as a service.
context.startService(it);
// But if for some reason you want to start an Activity, just do it like:
// context.startActivity(it);
}
}
在您的AndroidManifest.xml
上声明您的BroadcastReceiver
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="Some_ID" />
<category android:name="android.intent.category.default" />
</intent-filter>
</receiver>
您可以阅读更多有关警报的信息。
有关服务的详细信息
。
广播接收机
。
通知,以及。
关于通知
也可能是一本有趣的读物