Android—做一个持续的的服务—闹钟服务

这里写图片描述

设计思路如上图

package com.example.demo35;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;

import com.example.demo35.receiver.MessageReceiver;
import com.example.demo35.service.MessageService;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        /*//先得到闹铃管理器
        AlarmManager manager=(AlarmManager)
                getSystemService(ALARM_SERVICE);
       //设置下一次闹铃的时间
        //SystemClock.elapsedRealtime();  开机到现在的毫秒数
        //long timer=SystemClock.elapsedRealtime()+ 1000*10;
        long timer=System.currentTimeMillis()+ 1000*10;

        Intent i=new Intent(this,MessageReceiver.class);
        PendingIntent pi=PendingIntent.getBroadcast(this, 0, i, 0);
        //manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timer, pi);
        manager.set(AlarmManager.RTC_WAKEUP, timer, pi);*/

        Intent i=new Intent(this,MessageService.class);
        this.startService(i);
    }
}
package com.example.demo35.receiver;

import com.example.demo35.service.MessageService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MessageReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "呼叫我了", Toast.LENGTH_SHORT).show();
        Intent i=new Intent(context,MessageService.class);
        context.startService(i);
    }

}
package com.example.demo35.service;

import java.util.Date;

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 com.example.demo35.receiver.MessageReceiver;

public class MessageService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {

        return null;
    }


    @Override
    public void onCreate() {
        Log.i("messageService", "onCreate");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("messageService", "onStartCommand"+new Date().toLocaleString());
        // 耗时的工作可以放到线程里面执行
        new Thread(){
            public void run() {

            }
            ;
        }.start();

        AlarmManager manager=(AlarmManager)
                getSystemService(ALARM_SERVICE);
       //设置下一次闹铃的时间
        //SystemClock.elapsedRealtime();  开机到现在的毫秒数
        long timer=SystemClock.elapsedRealtime()+ 1000*4;
        //long timer=System.currentTimeMillis()+ 1000*10;
        Intent i=new Intent(this,MessageReceiver.class);
        PendingIntent pi=PendingIntent.getBroadcast(this, 0, i, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timer, pi);
        //manager.set(AlarmManager.RTC_WAKEUP, timer, pi);
        return super.onStartCommand(intent, flags, startId);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo35"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.demo35.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.demo35.MessageReceiver"
            android:label="@string/title_activity_message_receiver" >
        </activity>
        <!-- 注册广播接收器 -->
        <receiver 
            android:name="com.example.demo35.receiver.MessageReceiver"></receiver>
        <service 
            android:name="com.example.demo35.service.MessageService"></service>
    </application>

</manifest>

最后说明一下闹钟类型

关于闹铃类型:
AlarmManager.RTC,硬件闹钟,不唤醒手机(也可能是其它设备)休眠;当手机休眠时不发射闹钟。

AlarmManager.RTC_WAKEUP,硬件闹钟,当闹钟发躰时唤醒手机休眠;

AlarmManager.ELAPSED_REALTIME,真实时间流逝闹钟,不唤醒手机休眠;当手机休眠时不发射闹钟。

AlarmManager.ELAPSED_REALTIME_WAKEUP,真实时间流逝闹钟,当闹钟发躰时唤醒手机休眠;

RTC闹钟和ELAPSED_REALTIME最大的差别就是前者可以通过修改手机时间触发闹钟事件,后者要通过真

实时间的流逝,即使在休眠状态,时间也会被计算。

假如使用System.currentTimeMillis()计算闹铃时间,需要使用RTC 或者 RTC_WAKEUP

在android中, 如何使用持续的service?
1) service + thread + while + sleep: 不够持续的原因在于线程的级别比较低
2) activity—>service—>闹铃—->唤醒cpu—->广播接收器—–>service

注意, 在4.4版本中, 闹铃有可能时间不够准确, 因为在4.4中, 为了省电, 有可能会把所有的闹铃集中在一起
进行执行, 这样时间就会有所延误

如果要保证时间的绝对准确, 建议使用
manager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
timer, pi);
去替代set

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个简单的Android闹钟程序的实现: 1. 首先,在Android Studio中创建一个新项目,并添加一个Activity。 2. 在Activity的布局文件中添加一个时间选择器和一个按钮,用于设置闹钟的时间和启动闹钟。 3. 在Activity中获取时间选择器和按钮,并设置按钮的点击事件。 4. 在按钮的点击事件中,获取时间选择器中选择的时间,并将其转换为Calendar对象。 5. 使用AlarmManager类来设置闹钟,在AlarmManager中设置闹钟的时间和触发的事件。 6. 在触发事件中,创建一个Notification对象,并使用NotificationManager类来显示通知。 下面是具体的代码实现: activity_main.xml布局文件: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TimePicker android:id="@+id/timePicker" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/startButton" android:text="Start Alarm" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> ``` MainActivity.java代码文件: ```java public class MainActivity extends AppCompatActivity { private TimePicker timePicker; private Button startButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timePicker = findViewById(R.id.timePicker); startButton = findViewById(R.id.startButton); startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int hour = timePicker.getHour(); int minute = timePicker.getMinute(); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, 0); Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); Toast.makeText(MainActivity.this, "Alarm set for " + hour + ":" + minute, Toast.LENGTH_SHORT).show(); } }); } } ``` AlarmReceiver.java代码文件: ```java public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "alarm_channel") .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Alarm") .setContentText("Wake up!") .setPriority(NotificationCompat.PRIORITY_HIGH); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(0, builder.build()); } } ``` 在AndroidManifest.xml文件中添加AlarmReceiver的声明: ```xml <receiver android:name=".AlarmReceiver"/> ``` 注意:在Android 8.0及以上版本中,需要为通知添加一个通知渠道。可以在MainActivity的onCreate方法中添加以下代码: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("alarm_channel", "Alarm", NotificationManager.IMPORTANCE_HIGH); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } ``` 这样就完成了一个简单的Android闹钟程序的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值