0

嗨,我已经创建了我的第一个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");

}

}

4

1 回答 1

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>

祝你好运 ;)

于 2013-07-07T13:10:10.850 回答