GVKun编程网logo

Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏(android实现闹钟提醒)

12

本文将介绍Android使用AlarmManager和NotificationManager来实现闹钟和通知栏的详细情况,特别是关于android实现闹钟提醒的相关信息。我们将通过案例分析、数据研究等

本文将介绍Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏的详细情况,特别是关于android实现闹钟提醒的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于AlarmManager 类的应用(实现闹钟功能)、AlarmManager和BroadcastReceiver的LocalNotification未在Android O(oreo)中启动、AlarmManager类的应用(实现闹钟功能)、Android AlarmManager和服务问题的知识。

本文目录一览:

Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏(android实现闹钟提醒)

Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏(android实现闹钟提醒)

实现闹钟运行的效果如下:

这里写图片描述

 

这里写图片描述

通知栏的运行后效果图如下:

这里写图片描述

布局文件(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
  android:layout_width="match_parent" android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:orientation="vertical"
  tools:context="com.example.g150825_android28.MainActivity">
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置闹钟(一次)"
    android:onClick="setAlarmOne"
    />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置闹钟(周期)"
    android:onClick="setAlarm"
    />
  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="取消周期闹钟"
    android:onClick="cancelAlarm"
    />
  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="发送通知"
    android:onClick="send"
    />
</LinearLayout>

activity_ring.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_ring"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.g150825_android28.RingActivity">
  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="停止"
    android:onClick="stop"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="慈禧太后青霜来了,赶紧起床!"
    android:id="@+id/textView"
    android:textSize="30sp"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp" />
</RelativeLayout>

RingActivity

package com.example.g150825_android28;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class RingActivity extends AppCompatActivity {
  private MediaPlayer mediaPlayer;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ring);
    mediaPlayer = MediaPlayer.create(this,R.raw.sqbm);
    mediaPlayer.start();
  }
  public void stop(View view){
    mediaPlayer.stop();
    finish();
  }
}

MyReceiver

package com.example.g150825_android28;
import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends broadcastReceiver {
  public MyReceiver() {
  }
  @Override
  public void onReceive(Context context,Intent intent) {
    if("com.example.g150825_android28.RING".equals(intent.getAction())){
      Toast.makeText(context,"闹钟响了",Toast.LENGTH_SHORT).show();
      //跳转到Activity
      Intent intent1=new Intent(context,RingActivity.class);
      //设置标志位(Flag)
      intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent1);
    }
  }
}

清单文件(AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.g150825_android28">
  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <receiver
      android:name=".MyReceiver"
      android:enabled="true"
      android:exported="true">
      <intent-filter>
        <action android:name="com.example.g150825_android28.RING" />
      </intent-filter>
    </receiver>
    <activity android:name=".RingActivity"
      android:theme="@style/Theme.AppCompat.Dialog"
      ></activity>
  </application>
</manifest>

以上所述是小编给大家介绍的Android 使用AlarmManager和notificationmanager来实现闹钟和通知栏,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

AlarmManager 类的应用(实现闹钟功能)

AlarmManager 类的应用(实现闹钟功能)

1、AlarmManager,顾名思义,就是 “提醒”,是 Android 中常用的一种系统级别的提示服务,可以实现从指定时间开始,以一个固定的间隔时间执行某项操作,所以常常与广播(Broadcast)连用,实现闹钟等提示功能

2、AlarmManager 的常用方法有三个:

(1)set(int type,long startTime,PendingIntent pi)

该方法用于设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作。

(2)setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法用于设置重复闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟首次执行时间,第三个参数表示闹钟两次执行的间隔时间,第三个参数表示闹钟响应动作。

(3)setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法也用于设置重复闹钟,与第二个方法相似,不过其两个闹钟执行的间隔时间不是固定的而已。

3、三个方法各个参数详悉:

(1)int type: 闹钟的类型,常用的有 5 个值:AlarmManager.ELAPSED_REALTIME、 AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、 AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。

AlarmManager.ELAPSED_REALTIME 表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始),状态值为 3;

AlarmManager.ELAPSED_REALTIME_WAKEUP 表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间,状态值为 2;

AlarmManager.RTC 表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间,状态值为 1;

AlarmManager.RTC_WAKEUP 表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,状态值为 0;

AlarmManager.POWER_OFF_WAKEUP 表示闹钟在手机关机状态下也能正常进行提示功能,所以是 5 个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间,状态值为 4;不过本状态好像受 SDK 版本影响,某些版本并不支持;

(2)long startTime: 闹钟的第一次执行时间,以毫秒为单位,可以自定义时间,不过一般使用当前时间。需要注意的是,本属性与第一个属性(type)密切相关,如果第一个参数对 应的闹钟使用的是相对时间(ELAPSED_REALTIME 和 ELAPSED_REALTIME_WAKEUP),那么本属性就得使用相对时间(相对于 系统启动时间来说),比如当前时间就表示为:SystemClock.elapsedRealtime ();如果第一个参数对应的闹钟使用的是绝对时间 (RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本属性就得使用绝对时间,比如当前时间就表示 为:System.currentTimeMillis ()。

(3)long intervalTime:对于后两个方法来说,存在本属性,表示两次闹钟执行的间隔时间,也是以毫秒为单位。

(4)PendingIntent pi: 是闹钟的执行动作,比如发送一个广播、给出提示等等。PendingIntent 是 Intent 的封装类。需要注意的是,如果是通过启动服务来实现闹钟提 示的话,PendingIntent 对象的获取就应该采用 Pending.getService (Context c,int i,Intent intent,int j) 方法;如果是通过广播来实现闹钟提示的话,PendingIntent 对象的获取就应该采用 PendingIntent.getBroadcast (Context c,int i,Intent intent,int j) 方法;如果是采用 Activity 的方式来实现闹钟提示的话,PendingIntent 对象的获取就应该采用 PendingIntent.getActivity (Context c,int i,Intent intent,int j) 方法。如果这三种方法错用了的话,虽然不会报错,但是看不到闹钟提示效果。

4、 AlarmManager 使用示例:利用用户自定义广播实现闹钟功能,从当前时间开始,每隔 10 分钟提示一次

(1)实现原理:在 SendActivity.java 中定义一个 AlarmManager 对象,指定该对象从当前时间开始,每隔 10 分钟向名为 “MYALARMRECEIVER” 的广播接收器发出一条广播,附加消息内容为 “你该打酱油了”;创建一个名为 MyReceiver 的广播接收器,在其 onReceive 方法中获取 Intent 对象传过来的值(“你该打酱油了”)并用一个 Toast 组件显示出来;在 AndroidManifest.xml 文件中注册 SendActivity 类和广播接收器类 MyReceiver,设置 MyReceiver 的 action 的值为 “MYALARMRECEIVER”

(2)代码实现:

第一步:创建广播接收类 MyReceiver.java,在其 onReceive 方法中获取 Intent 的附加信息 msg,并用 Toast 组件显示

[java] view plain copy
  1. public void onReceive(Context context,Intent intent){  
  2.      String msg = intent.getStringExtra("msg");  
  3.      Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();  
  4. }  

第二步:在 AndroidManifest.xml 中注册广播接收类 MyReceiver.java,设置其 action 值为 “MYALARMRECEIVER”

[java] view plain copy
  1. <receiver android:name=".MyReceiver">  
  2.    <intent-filter>  
  3.       <action android:name="MYALARMRECEIVER" />  
  4.    </intent-filter>  
  5. </receiver>  

第三步:创建 SendActivity.java,用于设置闹钟,定时发出广播

[java] view plain copy
  1. // 创建 Intent 对象,action 指向广播接收类,附加信息为字符串 “你该打酱油了”  
  2.   
  3. Intent intent = new Intent("MYALARMRECEIVER");  
  4.   
  5. intent.putExtra("msg","你该打酱油了");  
  6.   
  7. // 创建 PendingIntent 对象封装 Intent,由于是使用广播,注意使用 getBroadcast 方法  
  8.   
  9. PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);  
  10.   
  11. // 获取 AlarmManager 对象  
  12.   
  13. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);  
  14.   
  15. // 设置闹钟从当前时间开始,每隔 10 分钟执行一次 PendingIntent 对象,注意第一个参数与第二个参数的关系  
  16.   
  17. am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentMillis(),600*1000,pi);  

第四步:在 AndroidManifest 中为 SendActivity.java 注册

[java] view plain copy
  1. <activity android:name=".SendActivity" /> 

AlarmManager和BroadcastReceiver的LocalNotification未在Android O(oreo)中启动

AlarmManager和BroadcastReceiver的LocalNotification未在Android O(oreo)中启动

在SDK 26之前,我已经在机器人上运行了本地通知

但是在Android O中我收到了以下警告,广播接收器没有被解雇.

W/broadcastQueue: Background execution not allowed: receiving Intent { act=package.name.action.LOCAL_NOTIFICATION cat=[com.category.localnotification] flg=0x14 (has extras) } to package.name/com.category.localnotifications.localnotificationReceiver

从我所看到的广播接收器在Android O中受到更多限制,但如果是这样,即使主要活动没有运行,我应该如何安排广播?

我应该使用服务而不是接收器吗?

这是AlarmManager启动代码:

public void Schedule(String aID, String aTitle, String aBody, int aNotificationCode, long aEpochTime)
{
    Bundle lExtras = new Bundle();
    lExtras.putInt("icon", f.getDefaultIcon());
    lExtras.putString("title", aTitle);
    lExtras.putString("message", aBody);
    lExtras.putString("id", aID);
    lExtras.putInt("requestcode", aNotificationCode);

    Intent lIntent = 
      new Intent(localnotificationScheduler.ACTION_NAME)
      .addCategory(NotificationsUtils.LocalNotifCategory)
      .putExtras(lExtras);

    PendingIntent lPendIntent = PendingIntent.getbroadcast(f.getApplicationContext(), aNotificationCode,
                                                           lIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager lAlarmMgr = (AlarmManager) f.getSystemService(Context.ALARM_SERVICE);
    lAlarmMgr.set(AlarmManager.RTC, 1000, lPendIntent);
}

这是接收者代码:

public class localnotificationReceiver extends broadcastReceiver {

public static native void   nativeReceivelocalnotification (String aID, String aTitle, String aMessage, boolean aOnForeground );

/** This method receives the alarms set by localnotificationScheduler,
*   notifies the CAndroidNotifications c++ class, and (if needed) ships a notification banner 
*/
@Override
public void onReceive(Context aContext, Intent aIntent)
{
    Toast.makeText(context, text, duration).show();
}

}

Android清单:

<receiver android:name="com.category.localnotifications.localnotificationReceiver">
        <intent-filter>
            <action android:name="${applicationId}.action.LOCAL_NOTIFICATION" />
            <category android:name="com.category.localnotification" />
        </intent-filter>
    </receiver>

解决方法:

Android O是迄今为止最新的.因此,我尝试消化并提供尽可能准确的信息.

从https://developer.android.com/about/versions/oreo/background.html#broadcasts起

>针对Android 8.0或更高版本的应用无法再在其清单中为隐式广播注册广播接收器.

>应用程序可以在运行时使用Context.registerReceiver()为任何广播注册接收器,无论是隐式还是显式.

>应用可以继续在其清单中注册显式广播.

此外,在https://developer.android.com/training/scheduling/alarms.html中,示例使用显式广播,并未提及有关Android O的任何特殊内容.

我建议你尝试一下如下的明确广播吗?

public static void startAlarmbroadcastReceiver(Context context, long delay) {
    Intent _intent = new Intent(context, AlarmbroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getbroadcast(context, 0, _intent, 0);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    // Remove any prevIoUs pending intent.
    alarmManager.cancel(pendingIntent);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pendingIntent);        
}

AlarmbroadcastReceiver

public class AlarmbroadcastReceiver extends broadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
    }

}

在AndroidManifest中,只需将类定义为

<receiver android:name="org.yccheok.AlarmbroadcastReceiver" >
</receiver>

AlarmManager类的应用(实现闹钟功能)

AlarmManager类的应用(实现闹钟功能)

1、AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,可以实现从指定时间开始,以一个固定的间隔时间执行某项操作,所以常常与广播(Broadcast)连用,实现闹钟等提示功能

2、AlarmManager的常用方法有三个:

(1)set(int type,long startTime,PendingIntent pi)

该方法用于设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作。

(2)setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法用于设置重复闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟首次执行时间,第三个参数表示闹钟两次执行的间隔时间,第三个参数表示闹钟响应动作。

(3)setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);

该方法也用于设置重复闹钟,与第二个方法相似,不过其两个闹钟执行的间隔时间不是固定的而已。

3、三个方法各个参数详悉:

(1)int type:闹钟的类型,常用的有5个值:AlarmManager.ELAPSED_REALTIME、AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。

AlarmManager.ELAPSED_REALTIME表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始),状态值为3;

AlarmManager.ELAPSED_REALTIME_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间,状态值为2;

AlarmManager.RTC表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间,状态值为1;

AlarmManager.RTC_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,状态值为0;

AlarmManager.POWER_OFF_WAKEUP表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间,状态值为4;不过本状态好像受SDK版本影响,某些版本并不支持;

(2)long startTime:闹钟的第一次执行时间,以毫秒为单位,可以自定义时间,不过一般使用当前时间。需要注意的是,本属性与第一个属性(type)密切相关,如果第一个参数对应的闹钟使用的是相对时间(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那么本属性就得使用相对时间(相对于系统启动时间来说),比如当前时间就表示为:SystemClock.elapsedRealtime();如果第一个参数对应的闹钟使用的是绝对时间(RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本属性就得使用绝对时间,比如当前时间就表示为:System.currentTimeMillis()。

(3)long intervalTime:对于后两个方法来说,存在本属性,表示两次闹钟执行的间隔时间,也是以毫秒为单位。

(4)PendingIntent pi:是闹钟的执行动作,比如发送一个广播、给出提示等等。PendingIntent是Intent的封装类。需要注意的是,如果是通过启动服务来实现闹钟提示的话,PendingIntent对象的获取就应该采用Pending.getService(Context c,int i,Intent intent,int j)方法;如果是通过广播来实现闹钟提示的话,PendingIntent对象的获取就应该采用PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法;如果是采用Activity的方式来实现闹钟提示的话,PendingIntent对象的获取就应该采用PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。如果这三种方法错用了的话,虽然不会报错,但是看不到闹钟提示效果。

4、 AlarmManager使用示例:利用用户自定义广播实现闹钟功能,从当前时间开始,每隔10分钟提示一次

(1)实现原理:在SendActivity.java中定义一个AlarmManager对象,指定该对象从当前时间开始,每隔10分钟向名为“MYALARMRECEIVER”的广播接收器发出一条广播,附加消息内容为“你该打酱油了”;创建一个名为MyReceiver的广播接收器,在其onReceive方法中获取Intent对象传过来的值(“你该打酱油了”)并用一个Toast组件显示出来;在AndroidManifest.xml文件中注册SendActivity类和广播接收器类MyReceiver,设置MyReceiver的action的值为“MYALARMRECEIVER”

(2)代码实现:

第一步:创建广播接收类MyReceiver.java,在其onReceive方法中获取Intent的附加信息msg,并用Toast组件显示

public void onReceive(Context context,Intent intent){
     String msg = intent.getStringExtra("msg");
     Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
}

第二步:在AndroidManifest.xml中注册广播接收类MyReceiver.java,设置其action值为“MYALARMRECEIVER”

<receiver android:name=".MyReceiver">
   <intent-filter>
      <action android:name="MYALARMRECEIVER" />
   </intent-filter>
</receiver>

第三步:创建SendActivity.java,用于设置闹钟,定时发出广播

//创建Intent对象,action指向广播接收类,附加信息为字符串“你该打酱油了”
Intent intent = new Intent("MYALARMRECEIVER");
intent.putExtra("msg","你该打酱油了");
//创建PendingIntent对象封装Intent,由于是使用广播,注意使用getBroadcast方法
PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);
//获取AlarmManager对象
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
//设置闹钟从当前时间开始,每隔10分钟执行一次PendingIntent对象,注意第一个参数与第二个参数的关系
am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentMillis(),600*1000,pi);

第四步:在AndroidManifest中为SendActivity.java注册

<activity android:name=".SendActivity" />

Android AlarmManager和服务问题

Android AlarmManager和服务问题

我的应用程序中有一些文件,但是现在只有3个非常重要.这是一个带有警报声和通知的提醒应用程序.
我有一个maincode.java文件,其中包含一个复选框及其侦听器.如果用户在复选框中签入,则AlarmManager将意图发送到AlarmReceiver.java,后者将启动MyService.java. MyService java包含有关播放声音的代码.代码是不完整的.
MyService.java:

public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

    player = MediaPlayer.create(this, R.raw.sound);
    player.setLooping(false); // Set looping
}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
    player.stop();
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
    player.start();
}

AlarmReceiver.java:

public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

    player = MediaPlayer.create(this, R.raw.sound);
    player.setLooping(false); // Set looping

maincode.java的重要部分:

    cb1 = (CheckBox) findViewById(R.id.CheckBox01);
    cb1.setonCheckedchangelistener(new CompoundButton.OnCheckedchangelistener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
        {           
            if (cb1.isChecked()) 
                {
                 if (GlobalVars.getHourOfDay() >= 0) 
                 {
                     Toast.makeText(maincode.this, "ok", Toast.LENGTH_SHORT).show();
                     rem1.setText(GlobalVars.getReminder1name());
                        Intent intent = new Intent(maincode.this, AlarmReceiver.class);
                        PendingIntent pendingIntent = PendingIntent.getbroadcast(bInsulinReminder.this, 0,
                          intent, PendingIntent.FLAG_UPDATE_CURRENT);
                        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                        Calendar cal = Calendar.getInstance();
                        cal.set(Calendar.HOUR_OF_DAY, GlobalVars.getHourOfDay());
                        cal.set(Calendar.MINUTE, GlobalVars.getMinute());
                        cal.set(Calendar.SECOND, 0);
                        cal.set(Calendar.MILLISECOND, 0);
                        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+ 3000, 6000, pendingIntent);

                 }
                 Toast.makeText(maincode.this, "Checked", Toast.LENGTH_SHORT).show();
                } else {
                    rem1.setText("No reminder set");
                    Toast.makeText(maincode.this, "Not checked", Toast.LENGTH_SHORT).show();
                }
        }

        });

(rem1是提醒按钮,其文本取决于用户所需名称的名称)

该代码的问题是,如果我启动警报,我将无法停止.我知道MyService.java中有player.stop()命令,但如何从未选中复选框的maincode.java末尾调用它呢?

解决方法:

不,您不能直接从听众那里做到这一点.您可以通过以下方式禁用警报:

Intent intent = new Intent(maincode.this, AlarmReceiver.class);
PendingIntent pendingIntent =  PendingIntent.getbroadcast(bInsulinReminder.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingItem.cancel();
alarmManager.cancel(pendingItem);

或者(如果我想)AlarmReceiver是broadcastReceiver的实现,并且从onReceive方法中启动MyService,它是Service类的实现.

因此,如果要从maincode.java侦听器内部停止此警报,可以通过重新创建在AlarmReceiver中使用的PendingIntent并执行stopService方法来停止MyService.

希望能有所帮助.

关于Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏android实现闹钟提醒的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于AlarmManager 类的应用(实现闹钟功能)、AlarmManager和BroadcastReceiver的LocalNotification未在Android O(oreo)中启动、AlarmManager类的应用(实现闹钟功能)、Android AlarmManager和服务问题的相关信息,请在本站寻找。

本文标签: