0

我制作了一个 android 应用程序,它在一天中的特定时间(比如下午 6:15:00)显示通知消息。为此,我使用了CalendarAlarmManager类。

但是,当我在手机上安装该应用程序进行测试时,它会在指定时间向我显示消息/通知,但在任何时候都会继续显示相同的内容。我的意思是通知会在我的手机中持续出现虽然我已经在我的应用程序中设置了时间。

这是我的代码 1) Activity 类

import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;


public class MainActivity extends Activity 
 { 

  private PendingIntent pendingIntent;

@Override
public void onCreate(Bundle savedInstanceState) 
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.DAY_OF_MONTH, 13);

calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 48);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);

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

} //end onCreate

 }

2) 接收器

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

   }

 }

3) 服务等级

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

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

}

我在 manifest.xml 文件中添加了服务和接收器。请问谁能告诉我应该怎么做才能在一整天内只显示一次通知?

4

3 回答 3

1

试试这对我来说工作正常:

    Calendar cal = Calendar.getInstance();

    cal.set(Calendar.HOUR_OF_DAY, 11);
    cal.set(Calendar.MINUTE, 54);
    cal.set(Calendar.SECOND, 0);
    ca.set(Calendar.MILLISECOND, 0);
    Intent intent = new Intent(getBaseContext(), Receiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            getBaseContext(), 1, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            pendingIntent);
于 2013-07-18T06:24:55.617 回答
1

我从来没有像这样使用过 BroadcastReceiver

Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);

所以我怀疑BroadcastReceiver的用法是不是错了,通常我们使用BroadcastReceiver如下:

<receiver android:name=".MyReceiver">
   <intent-filter>
        <action android:name="mypackage.START_ALARM" ></action>
   </intent-filter>
</receiver> 

Intent i = new Intent("mypackage.START_ALARM");
于 2013-07-18T02:42:42.417 回答
1

我猜原因是 onCreate 方法被多次调用,所以你的 alarmManager.set 被多次调用。例如,无论何时更改屏幕方向,该活动都会被销毁,并通过 onCreate() 方法启动一个新活动。因此,每次您旋转屏幕时,该活动将被销毁,并通过 onCreate() 方法启动一个新活动,并再次调用 alarmManager.set。

您可以在 onSaveInstanceState(Bundle bundle) 方法中保存状态成员。每当屏幕旋转时,Android 都会调用此方法,并且将给定的捆绑包传递给 oncreate(Bundle bundle)。在您的 onCreate 中,在尝试调用 alarmManager.set 之前检查 Bundle 是否为空。

于 2013-07-18T05:22:52.820 回答