gpt4 book ai didi

android - 通过android中的服务播放闹钟

转载 作者:行者123 更新时间:2023-11-29 00:34:58 25 4
gpt4 key购买 nike

我是 android 的新手。我想在用户设置的时间到达时播放闹钟。我有闹钟 Activity 。代码如下:

Intent intent = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, (cal.getTimeInMillis()/1000)+(t*60*60),
pendingIntent);//t is the time which is contained in database

我的警报接收器 Activity 是这样的:

公共(public)类 AlarmReciever 扩展 Activity {

    private MediaPlayer mMediaPlayer;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.alarmrecieve);

Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
stopAlarm.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
mMediaPlayer.stop();
finish();
return false;
}
});
playSound(this, getAlarmUri());
}

private void playSound(Context context, Uri alert) {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (IOException e) {
System.out.println("OOPS");
}
}

//Get an alarm sound. Try for an alarm. If none set, try notification,
//Otherwise, ringtone.
private Uri getAlarmUri() {
Uri alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null) {
alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null) {
alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
return alert;
}
}

我的问题是,当用户输入时间时,警报 Activity 应该在后台运行。当时间到达时,警报应该启动。我听说过服务。但我不知道它在我的项目中的实现。谢谢提前

最佳答案

是的,您可以使用服务See here .并通过pending intent启动Service。而不是这样的 Activity

PendingIntent pendingIntent = PendingIntent.getService(this,12345, intent,PendingIntent.FLAG_CANCEL_CURRENT);

示例:

Intent myIntent = new Intent(Add.this, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 12345, myIntent, 0);

你的服务类将是这样的:

import java.util.Calendar;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyAlarmService extends Service {


@Override

public void onCreate() {

// TODO Auto-generated method stub

//Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();

}

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();

return null;

}
@Override

public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();

}
@Override

public void onStart(Intent intent, int startId) {

// TODO Auto-generated method stub

super.onStart(intent, startId);

Intent dialogIntent = new Intent(getBaseContext(),AlarmReciever.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);

}

}
@Override

public boolean onUnbind(Intent intent) {

// TODO Auto-generated method stub

Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();

return super.onUnbind(intent);

}

}

在服务中,调用你的 Activity AlarmReciever

关于android - 通过android中的服务播放闹钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13120796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com