每周 Android 触发通知

Fire notification every week in Android

我正在尝试在 Android 中发出通知,每周在选定的日期 9:40am 中显示。当用户按下此通知时,它应该将他带到片段 activity,我该如何实现?

我当前的代码:

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
static final int REQUEST_CODE = 0;
TextView tt;
final static int WEEK_Id=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Calendar calendar = Calendar.getInstance();
    String[] Categories = { "one","2","three"};
    tt=(TextView) findViewById(R.id.oooo);
    tt.setText(Categories[WEEK_Id]);

    // 8 AM Each day
    calendar.set(Calendar.HOUR_OF_DAY, 9);
    calendar.set(Calendar.MINUTE, 40);
    calendar.set(Calendar.SECOND, 0);
    Intent intent = new Intent(MainActivity.this, Receiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, REQUEST_CODE, intent, 0);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.setRepeating(am.RTC_WAKEUP, calendar.getTimeInMillis(), am.INTERVAL_DAY * 7, pendingIntent);
}
}

接收者代码:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class Receiver extends BroadcastReceiver {
static final int reqCode=0;

public Receiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
    showNotification(context);
}

public void showNotification(Context context) {
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, reqCode, intent, 0);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.food_recycleview)
            .setContentTitle("Title")
            .setContentText("one");
    mBuilder.setContentIntent(pi);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(reqCode, mBuilder.build());
}
}

我认为如果您在 9:40 上午之前访问 activity,我认为您的闹钟唯一响起的时间,否则您的闹钟是过去的一刻。

要让警报管理器在某个时刻响起,它必须是未来的某个时刻,并且从那时起它将重复。

所以你必须实例化日历,将它的时间设置为未来的下一个9:40,然后设置闹钟

Calendar calendar = Calendar.getInstance();
//verify if the current time is past 9:40, then add one day
if (calendar.get(Calendar.HOUR) > 9 
        || (calendar.get(Calendar.HOUR) == 9 && calendar.get(Calendar.MINUTE) >= 40))
    calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 0);

//然后在放置警报的意图时,您必须添加一个动作,以便您的广播接收器可以过滤它

Intent intent = new Intent(MainActivity.this, Receiver.class);
intent.setAction("com.name.package.ACTION_FOR_ALARM");
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(am.RTC_WAKEUP, calendar.getTimeInMillis(), am.INTERVAL_DAY * 7, pendingIntent);

在您的广播接收器中,您必须检查传入意图的操作

文件:MyBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    //recibo la alarma
    if(action.contains("com.name.package.ACTION_FOR_ALARM")){
        [...]   // <-- your code for the notification
    }
 }

并且不要忘记在清单中的 broadcastreceiver 中注册过滤器,这样您的应用程序就可以过滤您的 intent 操作:

<application>

    [...]

    <receiver android:name="MyBoradcastReceiver">
        <intent-filter >
            <action android:name="com.name.package.ACTION_FOR_ALARM" />
        </intent-filter>
    </receiver>

</application>