0
我有以下代碼。我在HelloAndroid2.java中設置併發出警報,2秒鐘後,AlarmReceiver.java顯示Toast消息。我想添加一些音頻到應用程序,但我有一些問題。Android MediaPlayer問題
HelloAndroid2.java:
public class HelloAndroid2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
}
}
AlarmReceiver.java:
public final class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
}
}
Q1:如果我把這個代碼放到HelloAndroid2.java:
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.onlyyou);
`mp.start();`
它工作正常。
如何將一個mp.stop()添加到AlarmReceiver.java?它不知道mp是什麼。
Q2:如果我在AlarmReceiver.java文件把代碼,有一個與getBaseContext問題()(說:未定義類型AlarmReceiver)
我應該使用一些其他的方面?
我希望沒關係,我提出了一個話題的問題。
謝謝! 在AlarmReceiver.java我把代碼: context.startService(new Intent(context,MyService.class)); 我創建了MyService.java代碼,我在這裏找到: http://developer.android.com/reference/android/app/Service.html – erdomester 2011-01-14 17:43:54