1

我想在一周中的特定日期发送通知,我的代码在当天有效,但每次打开智能手机/模拟器时都会收到通知。我该如何解决这个问题?

我的清单

 <receiver android:name=".AutoStart">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
        <receiver android:name=".Alarm"></receiver>
        <receiver android:name=".NotificationActivity"></receiver>

自动启动.java

    public class AutoStart extends BroadcastReceiver
{   
    Alarm alarm = new Alarm();
    @Override
    public void onReceive(Context context, Intent intent)
    {   
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            alarm.SetAlarm(context);
        }
    }
}

还有我的 Alarm.java

    @SuppressLint("Wakelock")
    public class Alarm extends BroadcastReceiver 
    {    
        static RSSFeed myRssFeed = null;
         @Override
         public void onReceive(Context context, Intent intent) 
         {   

             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
             PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
             wl.acquire();

             SetAlarm(context);

             wl.release();
         }

    public void SetAlarm(Context context)
     {
         AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, NotificationActivity.class);
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
         Calendar calendar = new GregorianCalendar();
         calendar.set(Calendar.DAY_OF_WEEK, 2);
         calendar.set(Calendar.HOUR_OF_DAY, 9);
         calendar.set(Calendar.MINUTE, 30);
         calendar.set(Calendar.SECOND, 0);
         am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); 
     }

     public void CancelAlarm(Context context)
     {
         Intent intent = new Intent(context, Alarm.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(sender);
     }
 }

谢谢

4

1 回答 1

0

因为您已经在清单中声明了 android.intent.action.BOOT_COMPLETED 权限。这意味着每当您的设备或手机重新启动时,它都会调用 BroadcastReceiver。

试试下面的。这是工作代码。它每 10 分钟唤醒一次 CPU,直到手机关机。

添加到 Manifest.xml:

...

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

...

<receiver  android:process=":remote" android:name="Alarm"></receiver>

... 代码:

package YourPackage;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.widget.Toast;

public class Alarm extends BroadcastReceiver 
{    
     @Override
     public void onReceive(Context context, Intent intent) 
     {   
         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
         wl.acquire();

         // Put here YOUR code.
         Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

         wl.release();
     }

 public void SetAlarm(Context context)
 {
     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
     Intent i = new Intent(context, Alarm.class);
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
     am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
 }

 public void CancelAlarm(Context context)
 {
     Intent intent = new Intent(context, Alarm.class);
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
     alarmManager.cancel(sender);
 }
 }

从服务设置警报:

 package YourPackage;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class YourService extends Service
{
Alarm alarm = new Alarm();
public void onCreate()
{
    super.onCreate();       
}

public void onStart(Context context,Intent intent, int startId)
{
    alarm.SetAlarm(context);
}

@Override
public IBinder onBind(Intent intent) 
{
    return null;
}

}

于 2013-03-22T14:19:42.050 回答