バックグラウンドプロセスの結果を取得するにはどうすればよいActivity
ですか?
私はOTAアップデートを使用しています。アプリはハンドラーを使用してバックグラウンドでそれを処理します。更新が完了すると、ユーザーにアラートダイアログを表示する必要があります。私はSettingsActivity
OTAで始めたところにそれを示すことができますが、ユーザーは他のどこにいてもかまいませんActivity
。
private void checkIsUpdated() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mDevice.getDevice().read(GattUUID.LS_DEVICE_INFORMATION, GattUUID.LC_FIRMWARE, new
BleDevice.ReadWriteListener() {
@Override
public void onEvent(ReadWriteEvent e) {
if (e.wasSuccess()) {
if (firmwareVersion.equals(e.data_string())) {
showAlertDialog("OTA update failed", "Device is not updated");
} else {
showAlertDialog("OTA update is successful.\nDevice is updated to new " +
"firmware!", "Device is updated");
}
Log.i("OTA", "Read firmware is new success");
} else {
Log.e("OTA", "Read firmware is new success");
}
}
});
}
}, 30000);
}
AlertDialog
コード
private void showAlertDialog(String message, String title) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.SwipeDialogLight);
builder.setTitle(title)
.setMessage(message)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
if (!alert.isShowing()) {
alert.show();
}
}
@ W4R10CKが述べたように、私もそう思いました。BroadcastReceiver
かもしれないではない非常に良いアイデア。しかし後で、私はそれについて考えました、そしてあなたがcheckIsUpdated
内部で関数を呼び出してそれからService
送信するならばBroadcast
、それは実行可能な解決策かもしれません。
したがって、基本的にはBaseActivity
クラスが必要であり、その中にはが必要ですBroadcastReceiver
。登録してonResume
、再度登録を解除する必要がありonPause
ます。
そしてabstract
、あなたActivity
もあなたのそれぞれでオーバーライドされるメソッドを持っている必要があります。
だからあなたBaseActivity
はこのように見えるかもしれません。
public abstract class BaseActivity extends AppCompatActivity {
private final Context context;
public BaseActivity() {
this.context = setContext();
}
protected abstract Context setContext();
@Override
public void onResume() {
super.onResume();
registerBroadcastReceiver();
}
@Override
public void onPause() {
super.onPause();
unRegisterBroadcastReceiver();
}
// Declare your BroadcastReceiver here
private class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
showAlertDialog(context);
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加