通过android中的服务播放警报

时间:2012-10-29 11:32:33

标签: android service alarmmanager

我是android的新手。我希望在用户设置的时间到达时发出警报。我有警报活动。 代码如下:

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 

我的警报接收器活动是这样的:

公共类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;
    }
}

我的问题是,当用户输入时间时,警报活动应该在后台运行。当时间到了,警报应该开始。我听说过服务。但我不知道它在我的项目中的实现。谢谢你预先

1 个答案:

答案 0 :(得分:2)

是的,您可以使用ServiceSee here。并通过待定意图启动服务。而不是像这样的活动

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

    }        

    }

在使用中,请调用您的活动AlarmReciever