我正在尝试在Android中发出通知,该通知会在每天上午9:40的每周显示。当用户按下此通知时,应该将他带到片段活动,我该如何实现?
我目前的代码:
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());
}
}
答案 0 :(得分:0)
我认为,如果您在上午9:40之前访问活动,那么您的闹钟响起的唯一时间,否则您将在过去发出警报。
警报管理员要在某个时刻离开,它必须是未来的一个时刻,从那时起它将会重复。
所以你必须实例化日历,将其时间设置为未来的下一个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);
在您的广播接收器中,您必须检查传入意图的操作
file: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中注册过滤器,这样您的应用就可以过滤您的意图操作:
<application>
[...]
<receiver android:name="MyBoradcastReceiver">
<intent-filter >
<action android:name="com.name.package.ACTION_FOR_ALARM" />
</intent-filter>
</receiver>
</application>