我试图允许用户离线时发表评论,这样每当打开wifi /互联网时,他的评论就会被发布。我使用的是BroadCastReceiver。但是我遇到的问题是,if (intent.getAction().equals("commentpost"))
如果我不在的话,它永远不会进入内部请尝试在单击后评论后再打开wifi。但是,if (wifi.isAvailable() || mobile.isAvailable())
每次打开wifi时它确实会进入内部。我无法理解我要去哪里。我的日志显示“网络可用”,但从不显示“发布评论”。
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();
}
}
}
}
显现
<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>
您需要将自定义操作添加到intent-filter
中BroadcastReceiver
。只有这样才能Intent
触发您的广告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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句