安卓闹钟不工作
我已经为此苦苦挣扎了几个小时。我还检查了文档和几个主题。我在两个主题中找到了这段代码,两个人都说该代码运行完美,但在我的计算机上却不行。第一个 Toast 出现,但第二个 Toast 从未出现。怎么了?
public class HelloAndroid2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
}
public final class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
}
}
}
I've been struggling with this for hours. I've also checked the documentation and several topics. I found this code in two topics, both guys said the code was working perfectly, but not on my computer. The first Toast appears, but the second one never. What is wrong?
public class HelloAndroid2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
}
public final class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,您不需要指定操作,因为您在意图中使用了类
AlarmReceiver.class
。在您的
AndroidManifest.xml
中,确保您在
标记内有一个接收器定义,例如:编辑:
好的,有两种方法可以使用广播接收器。
1) 从您提供的代码中,
AlarmReceiver.java
将包含:和
HelloAndroid2.java
:像这样,您可以将广播接收器设置为与
AndroidManifest.xml
和标签2) 第二种方式。通过这种方式,您只需使用 1 个文件
HelloWorld2.java
:在您的活动中,创建广播接收器并注册它。
Actually you dont need to specify the action since you use the class
AlarmReceiver.class
in the intent.In your
AndroidManifest.xml
, make sure you have a receiver definition within the<application>
tags, something like:<receiver android:name="AlarmReceiver">
Edit:
Ok there are 2 ways to use your broadcast receiver.
1) From the code you have provided,
AlarmReceiver.java
that will contains:and
HelloAndroid2.java
:Like this, you can set your broadcast receiver to work with the
AndroidManifest.xml
and the tag<receiver ...>
2)2nd way. With this way, you can use just 1 file
HelloWorld2.java
:In your activity, create your broadcast receiver and register it.
我遇到了同样的问题,直到我发现我将广播接收器放在了不同的包上,而不是通用的包上。
简单地更改
为:
I had the same problem until I found that I had put my Broadcast Receiver on a different package, not the general.
Simply changed:
for:
如果上述答案对您不起作用,那么还有另一种方法可以在
AlarmManager
触发过期警报时不接收任何回调。您只需检查一下这一点:通过在PendingIntent
实例化时发送错误的Intent
。例如,您希望在其中一个接收器上接收onReceive
调用,但您通过getActivity
或getService
实例化了PendingIntent
>,但你真正的意思是getReceiver
。创建
PendingIntent
实例时,有多种创建方法(getService
、getActivity
、getReceiver
、>getForegroundService
:如果您想要
Activity
意图的接收者,那么您:如果您想要
BroadcastReceiver
意图的接收者:如果您想要前台
Service
意图的接收者:如果您想要一个
Service
意图的接收者:另外,请确保您的意图指向正确的类(例如为 Activity 创建意图,如果您像这样错误地通过,您将不会收到任何电话:
我也发布了类似的答案此处
。HTH
If the answer above doesn't work for you then there is another way to not receive any callbacks when
AlarmManager
fires an expired alarm. You simply need to check this one out: by sending the wrongIntent
on instantiation ofPendingIntent
. For example you wanted to receive a callonReceive
on one of your receivers but you instantiated aPendingIntent
viagetActivity
orgetService
, but what you actually meant isgetReceiver
.When creating instance of
PendingIntent
, there are many ways to create it (getService
,getActivity
,getReceiver
,getForegroundService
:if you want
Activity
the receiver of the intent then you:if you want
BroadcastReceiver
the receiver of the intent:if you want a foreground
Service
the receiver of the intent:if you want a
Service
the receiver of the intent:Also, make sure you intents are pointing to the correct class. (e.g. creating intents for Activity, Service etc.). You will not receive any call if you pass wrongfully like this:
I also posted similar answer here.
HTH