如何使用具有特定日期的报警管理器?

时间:2013-07-07 11:38:16

标签: android eclipse alarmmanager

  

嗨我创建了我的第一个 Android应用来播放声音,特定时间我的代码很好,我添加了声音播放的日期和时间列表。当我在手机锁定为闹钟时,我更多地搜索了如何播放声音。最后,我达到了必须使用警报管理器。我需要有人帮助我,因为我做不到。正如我想要的那样,我在声音播放下面添加的特定时间。

注意:我使用接口类将调用 html 文件调用到android

public class WebAppInterface    {
Context mContext;
public  MediaPlayer mp = null;
 public static boolean checked = false;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
    mContext = c;
    mp = MediaPlayer.create(mContext,R.raw.sound);

}


@JavascriptInterface
public void playsound(String value  ) throws ParseException {


   //get current date time with Date()

    String dateStr = "0, 3, 11, 9, 7, 2013"; 

    SimpleDateFormat dateFormat = new SimpleDateFormat("ss,hh,mm,yyyy,MM,dd"); 
    Time time = new Time();
    System.out.println(dateFormat.format(time));

    //1day
     time.set(0, 3, 11, 9, 7, 2013);
     time.set(0, 3, 18, 9, 7, 2013);
     time.set(0, 5, 0, 9, 7, 2013);
     time.set(0, 12, 1, 9, 7, 2013);
     time.set(0, 3, 37, 9, 7, 2013);
     time.set(0, 7, 0, 9, 7, 2013);
     time.set(0, 8, 32, 9, 7, 2013);
     //2day
     time.set(0, 3, 11, 10, 7, 2013);
     time.set(0, 3, 18, 10, 7, 2013);
     time.set(0, 5, 0, 10, 7, 2013);
     time.set(0, 12, 1, 10, 7, 2013);
     time.set(0, 3, 37, 10, 7, 2013);
     time.set(0, 7, 0, 10, 7, 2013);
     time.set(0, 8, 31, 10, 7, 2013);

}

public class x extends Activity {

private MediaPlayer mediaplayer;
private String TAG;
Context mContext;
private IntentListener listener = new IntentListener();

WebAppInterface wb=null;

@Override
protected void onCreate(Bundle savedInstanceState) {

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


    //Call HTML Files
    WebView myWebView = (WebView) findViewById(R.id.web_engine);

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("file:///android_asset/index.html");

    //Initialise WebAppInterface and pass this ref..
    wb=new WebAppInterface(this);
    myWebView.addJavascriptInterface(wb, "Android");

}

}

1 个答案:

答案 0 :(得分:0)

首先你需要创建一个扩展BroadcastReceiver的类:

public class AlarmManagerBroadcastReciever 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, "YOUR TAG");
    // Acquire the lock
    wl.acquire();
    MediaPlayer mp=MediaPlayer.create(context,R.raw.<your audio>);
    mp.start();
    // Release the lock
    /// do your playing sound here
    wl.release();
}

public void SetAlarm(Context con, int id, int hour, int min) {
    AlarmManager am = (AlarmManager) con
            .getSystemService(Context.ALARM_SERVICE);
    Calendar cal = Calendar.getInstance();
    cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
            cal.get(Calendar.DAY_OF_MONTH), hour, min);
    Intent intent = new Intent(con, AlarmManagerBroadcastReciever.class);

    PendingIntent sender = PendingIntent.getBroadcast(con, id, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}

public void CancelAlarm(Context context, int size) {
            // canceling all alarms 
    for (int i = 0; i < size; i++) {
        Intent intent = new Intent(context,
                AlarmManagerBroadcastReciever.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, i,
                intent, 0);
        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }
}

}

在您的MainActivity中

创建此类的对象:

AlarmManagerBroadcastReciever alarm = new AlarmManagerBroadcastReciever();

并添加以下方法:

public void startTimer(int hour, int min, int id) {
    Context context = this.getApplicationContext();
    if (alarm != null) {
        alarm.SetAlarm(context, id, hour, min);
    } else {
        Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
    }
}

public void cancelTimer() {
    Context context = this.getApplicationContext();
    if (alarm != null) {
        alarm.CancelAlarm(context, <number of alarms>);
    } else {
        Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
    }
}

使用startTimer()设置特定的警报,并在broadcastReciever的onRecieve()中做你的事情。别忘了将broadcastReciever添加到清单文件中:

<receiver android:name="<package name>.AlarmManagerBroadcastReciever"
        >
    </receiver>
祝你好运;)