广播接收器和Android的alarmManager

编程入门 行业动态 更新时间:2024-10-12 05:44:19
本文介绍了广播接收器和Android的alarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用与broadcasReceiver报警马槽里。我尝试在教程红粉使用例如:系统服务和广播接收器。但是当我的时间结束后运行的例子,我还没有,我想展现的敬酒。

:在code是由以下code给出

我的主要活动:

公共类AlarmActivity延伸活动{    / **当第一次创建活动调用。 * /    @覆盖    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView(R.layout.activity_alarm);    }    公共无效startAlert(查看视图){        文字的EditText =(EditText上)findViewById(R.id.time);        INT I =的Integer.parseInt(text.getText()的toString());        意向意图=新意图(这一点,MyBroadcastReceiver.class);        的PendingIntent的PendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),234324243,​​意向,0);        AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);        alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()                +(I * 1000)的PendingIntent);        Toast.makeText(这一点,报警设置中的+ I +秒                Toast.LENGTH_LONG).show();    }}

我的广播接收器:

公共类MyBroadcastReceiver扩展广播接收器{    公共无效的onReceive(上下文的背景下,意图意图){        Toast.makeText(背景下,不要panik但你的时间到了!!!!                Toast.LENGTH_LONG).show();        //震动移动电话        振动器振动=(振动器)context.getSystemService(Context.VIBRATOR_SERVICE);        vibrator.vibrate(2000);    }}

我的主要Laout:

<?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android/apk/res/android              机器人:layout_width =match_parent              机器人:layout_height =match_parent              机器人:方向=垂直>    <的EditText            机器人:ID =@ + ID /时间            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:提示=秒数            安卓的inputType =numberDecimal>    < /&的EditText GT;    <按钮            机器人:ID =@ + ID / OK            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            安卓的onClick =startAlert            机器人:文字=启动计数器>    < /按钮>< / LinearLayout中>

和manifestfile:

<?XML版本=1.0编码=UTF-8&GT?;<清单的xmlns:机器人=htt​​p://schemas.android/apk/res/android    包=com.example.broadcastreceiver    安卓版code =1    机器人:=的versionName1.0>    <用途-SDK        安卓的minSdkVersion =8        机器人:targetSdkVersion =18/>    <使用许可权的android:NAME =android.permission.VIBRATE>    < /使用许可权>    <应用        机器人:allowBackup =真        机器人:图标=@绘制/ ic_launcher        机器人:标签=@字符串/ APP_NAME        机器人:主题=@风格/ AppTheme>        <活动            机器人:名字=com.example.broadcastreceiver.AlarmActivity            机器人:标签=@字符串/ APP_NAME>            &所述;意图滤光器>                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>            &所述; /意图滤光器>        < /活性GT;         <接收机器人:MyBroadcastReceiverNAME =>        < /接收器>    < /用途>< /清单>

解决方案

接收器必须从广播接收器延长

公共类MyBroadcastReceiver扩展广播接收器{    // ...}

另外要确保清单文件接收器名称是正确的。

I am trying to use an alarm manger with broadcasReceiver. I try to use the example gived in Tutorial: System Services and BroadcastReceiver . but when I run the example after the end of the time I haven't the toast that i would like to show. the code is given by the code below:

My main Activity:

public class AlarmActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alarm); } public void startAlert(View view) { EditText text = (EditText) findViewById(R.id.time); int i = Integer.parseInt(text.getText().toString()); Intent intent = new Intent(this, MyBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent); Toast.makeText(this, "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show(); } }

my broadCastReceiver:

public class MyBroadcastReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG).show(); // Vibrate the mobile phone Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(2000); } }

my main Laout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="schemas.android/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Number of seconds" android:inputType="numberDecimal" > </EditText> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startAlert" android:text="Start Counter" > </Button> </LinearLayout>

and the manifestfile:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="schemas.android/apk/res/android" package="com.example.broadcastreceiver" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.VIBRATE" > </uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.broadcastreceiver.AlarmActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyBroadcastReceiver" > </receiver> </application> </manifest>

解决方案

The receiver must extend from BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver { // ... }

Also be sure that receiver name in manifest file is correct.

更多推荐

广播接收器和Android的alarmManager

本文发布于:2023-11-27 12:54:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收器   Android   alarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!