通知未在指定时间出现

时间:2015-04-13 15:01:52

标签: java android android-notifications

这段代码有什么问题?它应该在指定的时间提出通知。我做了一些测试和记录,但没有任何改变 - 通知从未出现......

NotificationService.java

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

public class NotificationService extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String link = "http://www.login.lt/apdovanojimai/";
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(link));
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder n = new NotificationCompat.Builder(this)
                .setContentTitle("LOGIN 2015")
                .setContentText(getResources().getString(R.string.rate_at_login))
                .setSmallIcon(R.drawable.logo)
                .setContentIntent(pIntent)
                .setAutoCancel(true);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, n.build());
    }
} 

setAlarm():

private void setAlarm() {
    Intent myIntent = new Intent(this , NotificationService.class);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);

    Calendar date1 = Calendar.getInstance();
    date1.set(2015, 04, 16, 19, 00, 00);

    Calendar date2 = Calendar.getInstance();
    date2.set(2015, 04, 24, 19, 00, 00);

    alarmManager.set(AlarmManager.RTC_WAKEUP, date1.getTimeInMillis(), pendingIntent);
    alarmManager.set(AlarmManager.RTC_WAKEUP, date2.getTimeInMillis(), pendingIntent);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000 , pendingIntent);
}

MainActivity.java:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
        setAlarm();
    }

1 个答案:

答案 0 :(得分:1)

考虑到setAlarm()方法在YourActivity中:

private void setAlarm() {
 AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
 Intent intent = new Intent(YourActivity.this, AlarmReceiver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
 Calendar cal = Calendar.getInstance();
 cal.set(Calendar.HOUR_OF_DAY, 23);//set the hour which you want
 cal.set(Calendar.MINUTE, 56);// set the minute
 cal.set(Calendar.SECOND, 0);// set seconds
 alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}

在AlarmReceiver类中:

public class AlarmReceiver extends BroadcastReceiver {

  Context myContext;

  @Override
  public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
    myContext = context;
    showNotification();

  }

 public void showNotification()
  {
    String link = "http://www.login.lt/apdovanojimai/";
    Intent intent1 = new Intent(Intent.ACTION_VIEW);
    intent1.setData(Uri.parse(link));
    PendingIntent pIntent = PendingIntent.getActivity(myContext, 0, intent1, 0);

    NotificationCompat.Builder n = new NotificationCompat.Builder(myContext)
            .setContentTitle("LOGIN 2015")
            .setContentText("hello")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent)
            .setAutoCancel(true);

    NotificationManager notificationManager =
            (NotificationManager) myContext.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, n.build());
   }

}

确保将其添加到清单文件中:

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