简体   繁体   English

TIME_TICK操作未呼叫广播接收器

[英]TIME_TICK action not calling broadcast receiver

I have written a simple broadcast receiver responding to TIME_TICK action . 我编写了一个简单的广播接收器,以响应TIME_TICK动作。

When I add the action in the manifest file it is not calling the registered receiver but when I register the receiver in the java code it is being called. 当我在清单文件中添加操作时,它不是在调用已注册的接收者,而是在Java代码中将其注册为接收者时。 I have a simple onreceive method. 我有一个简单的onreceive方法。

public class mybroad extends BroadcastReceiver
{
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        Log.v("got", "broadcasted");
        Toast.makeText(arg0, "hurray broadcast got", Toast.LENGTH_LONG).show();


    }

}

receiver tag for manifest file 清单文件的接收者标签

<receiver android:name="com.example.chapbasic.mybroad" >
            <intent-filter>
                <action android:name="android.intent.action.TIME_TICK"></action>
            </intent-filter>
        </receiver>

when I operate with the following code it is working 当我使用以下代码进行操作时

public class broadact extends Activity
{
    IntentFilter ii;
    mybroad mb;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mybroad);
    ii=new IntentFilter("android.intent.action.TIME_TICK");
    mb=new mybroad();

}
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    registerReceiver(mb, ii);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    unregisterReceiver(mb);
}

Kindly update why it is not being called from the manifest file registration. 请更新清单文件注册中未调用它的原因。 thanks 谢谢

kindly go through the documentation that states. 请仔细阅读说明的文档。

You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver() . 您不能通过清单中声明的​​组件来接收此消息,只能通过向Context.registerReceiver()进行显式注册来实现。

That is why you are not able to receive it when doing through manifest file. 这就是为什么在清单文件中无法接收它的原因。 thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM