Android - 广播 Intent 中的自定义操作
Android - Custom action in Broadcast Intent
我正在尝试允许用户在离线时 post 发表评论,这样每当 wifi/internet 打开时,他的评论将是 posted.Fot 我正在使用 BroadCastReceiver.But 我遇到的问题是它永远不会进入 if (intent.getAction().equals("commentpost"))
如果我在点击 postcomment.However 后尝试打开 wifi,它确实会进入 if (wifi.isAvailable() || mobile.isAvailable())
每当我打开 wifi.I无法理解我要去哪里 wrong.My 日志显示 "Network Available" 但从未显示 "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();
}
}
}
}
清单
<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>
您需要将自定义操作添加到 BroadcastReceiver
的 intent-filter
中。只有这样 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>
我正在尝试允许用户在离线时 post 发表评论,这样每当 wifi/internet 打开时,他的评论将是 posted.Fot 我正在使用 BroadCastReceiver.But 我遇到的问题是它永远不会进入 if (intent.getAction().equals("commentpost"))
如果我在点击 postcomment.However 后尝试打开 wifi,它确实会进入 if (wifi.isAvailable() || mobile.isAvailable())
每当我打开 wifi.I无法理解我要去哪里 wrong.My 日志显示 "Network Available" 但从未显示 "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();
}
}
}
}
清单
<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>
您需要将自定义操作添加到 BroadcastReceiver
的 intent-filter
中。只有这样 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>