火 notification 每周Android

标签: Java Android
发布时间: 2017/3/19 14:07:06
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图使 notificationAndroid 表明平均每周在选定的一天,9:40 上午。当用户按下上这 notification ,它应该带他到 fragment 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());
}
}

解决方法 1:

我认为只有 time 你将闹钟会如果你访问 activity 前 9:40 上午,否则你给报警时刻 int 过去。

在某一时刻去报警管理器,一定要时刻未来,和从然后它将重复。

因此你必须实例化日历,设置其 time 下 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 为报警,你必须添加操作,所以您的广播的接收器可以 filter

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);

你广播接收机中,你必须检查传入的行动intent

文件︰ 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
    }
 }

别忘了要注册 filter 在你 broadcastreceivermanifest ,所以您的应用程序可以 filterintent 行动︰

<application>

    [...]

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

</application>
赞助商