关闭

Android Service实现闹钟

标签: Serviceandroid
1065人阅读 评论(0) 收藏 举报
分类:

1.MainActivity.class中开启服务

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, LongRunningService.class);
        startService(intent);
    }
}

2.LongRunningService.class中获取闹钟服务,并定时10s,然后启动AlarmActivity.class

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

import java.util.Date;

/**
 * Created by admin on 16-5-31.
 */
public class LongRunningService extends Service{
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("LongRunningService", "executed at " + new Date().toString());
            }
        }).start();
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int anHour = 10 * 1000;
        long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
        Intent i = new Intent(this, AlarmActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);//1min后返回执行
        return super.onStartCommand(intent, flags, startId);
    }
}

3.AlarmActivity.class中实例化一个对话框,并播放音乐

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class AlarmActivity extends Activity {

    MediaPlayer mAlarmMusic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAlarmMusic = MediaPlayer.create(this,R.raw.alarm);
        mAlarmMusic.setLooping(true);
        mAlarmMusic.start();
        new AlertDialog.Builder(AlarmActivity.this).setTitle("Alarm !!!")
                .setMessage("Time Out,GO!")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mAlarmMusic.stop();
                        AlarmActivity.this.finish();
                    }
                }).show();
    }
}

实现效果,如下图所示。

            


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:218781次
    • 积分:2388
    • 等级:
    • 排名:第16084名
    • 原创:75篇
    • 转载:3篇
    • 译文:1篇
    • 评论:27条
    最新评论