NotificationBar条目未在定义的时间出现

时间:2014-12-20 10:06:57

标签: android broadcastreceiver android-notification-bar

我想定义通知出现的时间,所以我按照本教程

http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/

我测试了MyAlarmService.onStart中的代码,这个eams工作,我收到通知。 所以看来BroadcastReceiver无法正常工作。

我的代码看起来像这样

MainActivity:

/***********************************************************
 * Notification Bar Reminder setup
 **********************************************************/


Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.MONTH, 12);
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.DAY_OF_MONTH, 20);

calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 51);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);

Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

}

MyReceiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;



public class MyReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
       Intent service1 = new Intent(context, MyAlarmService.class);
       context.startService(service1);

    }  
}

MyAlarmService:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;


public class MyAlarmService extends Service
{

   private NotificationManager mManager;

    @Override
    public IBinder onBind(Intent arg0)
    {
       // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
       // TODO Auto-generated method stub 
       super.onCreate();
    }

   @SuppressWarnings("static-access")
   @Override
   public void onStart(Intent intent, int startId)
   {
       super.onStart(intent, startId);
       /*FR: this code works if you copy it direct to the mainActivity */
       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

       Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

       mManager.notify(0, notification);
    }

    @Override
    public void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.whoseturn"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/dummphoto"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Holo.Light.DarkActionBar">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyAlarmService"
                 android:enabled="true" />

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

</manifest>

1 个答案:

答案 0 :(得分:1)

Calendar.MONTH从零开始。也就是Calendar.JANUARY == 0。使用类常量更好;例如Calendar.DECEMBER。此外,Calendar.HOUR_OF_DAY基于24小时制。

我还要指出您使用AlarmManager.RTC设置闹钟,这不会唤醒您的设备以启动它。如果您需要这种行为,请改用AlarmManager.RTC_WAKEUP

一些测试说明:

  • Calendar.getInstance()返回已设置当前日期和时间的实例。您只需要设置那些现在不同的组件。

  • 如果在当前时间或之前设置了警报,则会立即触发警报。这是一个很好的测试,以确保正确设置接收器。