【发布时间】:2016-04-11 04:08:58
【问题描述】:
只要连接了互联网,我的应用就需要执行某些操作,反之亦然。但是,我需要在我的应用程序OUTSIDE 执行此操作。 stackoverflow 上有很多想法和帮助,但它总是 WITHIN 应用程序。我知道 AlarmManager 在应用程序之外工作,但如果我尝试每 5 分钟检查一次是否有互联网连接,它只会耗尽电池电量。我想要的是检查应用程序外部的连接以下载一些东西。
我还发现 Intent Service 也可以在应用程序之外工作。我试图把它放在唤醒广播接收器中。但是,它仍然没有被调用。
有人可以帮助我吗?谢谢!
这是我的唤醒广播接收器
public class ConnectivityOutsideAppReceiver extends WakefulBroadcastReceiver {
private static final String LOG_TAG = ConnectivityOutsideAppReceiver.class.getSimpleName();
private ConnectivityManager connectivityManager;
private static boolean connection = false;
@Override
public void onReceive(Context context, Intent intent){
ComponentName comp = new ComponentName(context.getPackageName(),
ConnectivityOutsideAppService.class.getName());
// Start the service, keeping the device awake while it is
// launching.
startWakefulService(context, (intent.setComponent(comp)
));
}
}
这是意图服务
公共类 ConnectivityOutsideAppService 扩展 IntentService {
private static final String LOG_TAG = ConnectivityOutsideAppService.class.getSimpleName();
private ConnectivityManager connectivityManager;
private static boolean connection = false;
private Context context;
public ConnectivityOutsideAppService() {
super(ConnectivityOutsideAppService.class.getSimpleName());
this.context = context;
}
@Override
protected void onHandleIntent(Intent intent) {
connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
checkConnectionOnDemand();
Log.e(LOG_TAG, " ## onHandleIntent##");
if (connection == true
&& intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
false)) {
Log.e(LOG_TAG, " ## connection == true ##");
connection = false;
} else if (connection == false
&& !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
false)) {
Log.e(LOG_TAG, " ## connection == false ##");
connection = true;
}
ConnectivityOutsideAppReceiver.completeWakefulIntent(intent);
}
public static boolean hasConnection() {
return connection;
}
private void checkConnectionOnDemand() {
Log.e(LOG_TAG, " ## checkConnectionOnDemand ##");
final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info == null || info.getState() != NetworkInfo.State.CONNECTED) {
if (connection == true) {
Log.e(LOG_TAG, " ## connection == true ##");
connection = false;
}
} else {
if (connection == false) {
Log.e(LOG_TAG, " ## connection == false ##");
connection = true;
}
}
}
}
这是我的 Android 清单
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<service
android:name="com.timetrackermobilelog.BusinessServices.ConnectivityOutsideAppService"
android:exported="false"/>
<receiver android:name="com.timetrackermobilelog.Utilities.ConnectivityOutsideAppService"
android:enabled="true"
android:process=":remote">
<intent-filter android:priority="1000" >
<action android:name="com.timetrackermobilelog.Utilities.ConnectivityOutsideAppService" />
<category android:name="com.Utilities" />
</intent-filter>
</receiver>
我尝试在 Activity 中注册接收器,但效果不佳。
IntentFilter intentFilter = new IntentFilter("com.pointwest.timetrackermobilelog.Utilities.ConnectivityOutsideAppReceiver");
ConnectivityOutsideAppReceiver connectivityOutsideAppReceiver = new ConnectivityOutsideAppReceiver();
registerReceiver(connectivityOutsideAppReceiver, intentFilter);
我有什么遗漏的吗?
【问题讨论】:
-
在
CONNECTIVITY_CHANGEaction 的清单中注册您的接收器,而不是您的自定义操作。 -
你调试过你的代码吗?你应该在哪里遇到问题..?
-
嗨@MikeM。我会把那个拿出来再回复你。
-
@VishalHalani。我已经这样做了,没有错误。未调用 ConnectivityOutsideAppService。
-
@MikeM。谢谢!有效!我会在这里写下答案。向你致敬!
标签: java android android-manifest android-intentservice android-broadcastreceiver