gpt4 book ai didi

android - 应用程序打开时警报管理器不断触发

转载 作者:行者123 更新时间:2023-11-30 03:05:51 25 4
gpt4 key购买 nike

我有一个问题,我希望有人能指出它发生的原因。我硬编码了我希望闹钟触发的日期,当第一次运行该应用程序时,它只会在那个时间点触发。但是,一旦触发警报,每次打开应用程序时,警报都会持续触发并显示通知。这只是一个小型应用程序测试,将用作更大应用程序的一部分,希望有人能帮助我

package com.example.alarmtest;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

private PendingIntent pendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Calendar calendar = Calendar.getInstance();


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

calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 26 );
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);

}



}

package com.example.alarmtest;


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("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
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);


return super.onStartCommand(intent, flags, startId);
}



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



}

package com.example.alarmtest;

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

public class MyReceiver extends BroadcastReceiver {

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

}

}

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.alarmtest.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>

<activity
android:name="com.example.alarmtest.NextActivity"
android:label="Next Screen">
</activity>

<service android:name="com.example.alarmtest.MyAlarmService"
android:enabled="true" />

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

</manifest>

最佳答案

我认为您的 MainActivity onCreate() 方法将在您重新打开应用程序的大部分时间被调用。因此,您将多次设置闹钟。以后的闹钟将设置为过去的时间(我假设您将第一个闹钟设置为当前时间)并将立即触发。

关于android - 应用程序打开时警报管理器不断触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21826968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com