Android警报启动时启动

时间:2013-03-22 14:15:48

标签: android notifications broadcastreceiver alarmmanager bootcompleted

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

我的清单

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

AutoStart.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);
     }
 }

谢谢

1 个答案:

答案 0 :(得分: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;
}

}