Android的 - 在广播意图自定义操作 [英] Android - Custom action in Broadcast Intent

查看:155
本文介绍了Android的 - 在广播意图自定义操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户发布评论时,他是这样的下线,每当无线/互联网接通他的评论将被posted.Fot,我使用BroadCastReceiver.But我遇到的问题是,它是永远不会在如果(intent.getAction()。等于(commentpost))如果我尝试点击postcomment.However后在WiFi开关它里面去如果(wifi.isAvailable()|| mobile.isAvailable())每当我在wifi.I切换不明白我要去的地方wrong.My日志显示网络可用,但从来没有显示发帖评论。

  commentpost.setOnClickListener(新View.OnClickListener()
        {
            @覆盖
            公共无效的onClick(查看视图)
            {
  意向意图=新的Intent();
                intent.setAction(commentpost);
                mContext.sendBroadcast(意向);
}
}
公共类NetworkChangeReceiver扩展广播接收器{    @覆盖
    公共无效的onReceive(最终上下文的背景下,最终的意图意图){
        最后ConnectivityManager connMgr =(ConnectivityManager)上下文
                .getSystemService(Context.CONNECTIVITY_SERVICE);        最后android.net.NetworkInfo无线= connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);        最后android.net.NetworkInfo移动= connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);        如果(wifi.isAvailable()|| mobile.isAvailable())
        {
           Log.e(网络可用,旗1号);
            如果(intent.getAction()。等于(commentpost)){
                Log.e(张贴评论,旗2号);
          发表评论();
            }
        }
    }
}

清单

 <接收机器人:名字=xyz.NetworkChangeReceiver机器人:启用=真正的>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.PHONE_STATE>< /作用>
                <作用机器人:名字=android.net.conn.CONNECTIVITY_CHANGE/>
                <作用机器人:名字=android.net.wifi.WIFI_STATE_CHANGED/>
            &所述; /意图滤光器>
        < /接收器>


解决方案

您需要将您的自定义操作添加到意图过滤器你的广播接收器。只有这样,该意图触发广播接收器

 <意向滤光器>
    <作用机器人:名字=android.intent.action.PHONE_STATE>< /作用>
    <作用机器人:名字=android.net.conn.CONNECTIVITY_CHANGE/>
    <作用机器人:名字=android.net.wifi.WIFI_STATE_CHANGED/>
    <作用机器人:名字=commentpost/>
&所述; /意图滤光器>

I am trying to allow user to post comment when he is offline such that whenever wifi/internet is turned on his comment will be posted.Fot that I am using BroadCastReceiver.But the issue i am having is that it is never going inside if (intent.getAction().equals("commentpost")) if i try switching on wifi after clicking on postcomment.However it does go inside if (wifi.isAvailable() || mobile.isAvailable()) whenever i switch on wifi.I failed to understand where i am going wrong.My log shows "Network Available" but never shows "posting comment".

    commentpost.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
  Intent intent = new Intent();
                intent.setAction("commentpost");
                mContext.sendBroadcast(intent);
}
}


public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        final ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable() || mobile.isAvailable())
        {
           Log.e("Network Available", "Flag No 1");
            if (intent.getAction().equals("commentpost")) {
                Log.e("posting comment", "Flag No 2");
          postComment();
            }
        }
    }
}

Manifest

<receiver android:name="xyz.NetworkChangeReceiver" android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"></action>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
            </intent-filter>
        </receiver>

解决方案

You need to add your custom action to the intent-filter of your BroadcastReceiver. Only then will that Intent trigger your BroadcastReceiver.

<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE"></action>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
    <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
    <action android:name="commentpost"/>
</intent-filter>

这篇关于Android的 - 在广播意图自定义操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆