KK的专栏

深夜 火车 汽笛 远方 梦想 自我实现 QQ534388733

Android Service实现闹钟

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

实现效果,如下图所示。

            


阅读更多
版权声明:本文为博主原创文章,转载注明出处 https://blog.csdn.net/chjr1000/article/details/51559347
文章标签: Service android
个人分类: android 中级
想对作者说点什么? 我来说一句

Android Service实现闹钟服务 源代码

2016年06月02日 28.96MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭