我已经制作了一个Android应用程序,它在当天的特定时间(例如下午6:15:00)显示通知消息。为此,我使用日历和 AlarmManager 类。
然而,当我在手机上安装应用程序进行测试时,它会在指定的时间向我显示消息/通知,但在任何时间都会继续显示相同的信息。我的意思是通知会在一天中持续显示即使我已经在我的应用程序中设置了它的时间,我的手机也是如此。
这是我的代码 1)活动类
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文件中添加了服务和接收器。请允许任何人告诉我如何在一整天内只显示一次通知?
答案 0 :(得分: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");
答案 1 :(得分:1)
我猜原因是onCreate方法被多次调用,所以你的alarmManager.set被多次调用。例如,每当屏幕方向改变时,该活动将被销毁,并且新活动将由onCreate()方法启动。因此,每次旋转屏幕时,活动都将被销毁,并且onCreate()方法启动新活动,并再次调用alarmManager.set。
您可以在onSaveInstanceState(Bundle bundle)方法中保存状态成员。每当屏幕旋转时Android都会调用此方法,并且给定的bundle bundle会传递给oncreate(Bundle bundle)。在onCreate中,在尝试调用alarmManager.set之前检查Bundle是否为空。
答案 2 :(得分: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);