自注册之日起六个月后显示Toast留言

时间:2018-03-14 19:10:26

标签: android alarmmanager

我希望在用户注册日期六个月后在我的应用中显示举报消息

例如,在2018/04/20注册的用户我希望在2018/10/20

显示吐司信息

这意味着,自注册之日起6个月后

在php中我可以使用此命令SELECT * FROM events WHERE event_date >= DATE(NOW() + INTERVAL 6 MONTH) - 在sqlite中我该怎么办?

1 个答案:

答案 0 :(得分:1)

首先创建BroadcastReceiver的子类:

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle=intent.getExtras();
        if (bundle!=null) {
            String key=bundle.getString("MyKey");
            if (key.equals("my_key")){
                Toast.makeText(context, "your message" , Toast.LENGTH_SHORT).show();
            }
        }   
    }
}

然后你需要创建Calendar和AlarmManager:

Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.MONTH, here insert your mount number "for exam 6 for 6 mount later ");
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
 Intent intent=new Intent(MainActivity.this,MyReceiver.class);
intent.putExtra("MyKey","my_key");
long futureInMillis = calendar.getTimeInMillis();
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
alarmManager.set(AlarmManager.RTC_WAKEUP,futureInMillis,pendingIntent);

并在清单中添加:

<receiver android:name=".MyReceiver"/>