刪除我有一個類 「ConnectivityChangeReceiver」 類,它擴展了BroadcastReceiver:廣播接收器不工作時,應用程序從最近的應用程序列表
public class ConnectivityChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intentPostTelemetry = new Intent(context.getApplicationContext(), PostTelemetryData.class);
intentPostTelemetry.putExtra("isNetworkConnected", isConnected(context));
context.startService(intentPostTelemetry);
}
public boolean isConnected(Context context) {
ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}
}
這ConnectivityChangeReceiver類調用PostTemetryData
public class PostTelemetryData extends IntentService {
public PostTelemetryData() {
super("PostTelemetryData");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
boolean isNetworkConnected = extras.getBoolean("isNetworkConnected");
if (isNetworkConnected == true) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("TelemetryInfo", 0);
String telemetryDetailsInfo = sharedPreferences.getString("TelemetryDetailsInfo", null);
if (telemetryDetailsInfo != null && !telemetryDetailsInfo.isEmpty()) {
saveTelemetryData(telemetryDetailsInfo);
}
}
// your code
}}
在清單中,我添加以下代碼:
<receiver android:name=".ConnectivityChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service android:name=".PostTelemetryData"/>
此連接接收器在連接更改時啓動。 當應用程序正在運行時,一切工作正常。但當應用程序關閉並從最近的應用程序列表中刪除時,然後廣播接收器不工作所以我不能保存數據。所以請幫助我擺脫這種情況。 在此先感謝。
那麼這個問題的解決方法是什麼。 – Ankon