私はアンドロイド開発にかなり慣れていません。
USBからシリアル接続に毎秒読み取るアプリがあります。その後、アプリは MainActivity の UI を新しいデータで更新します。私のアプリには、新しいデータで更新されるウィジェットもあります。これはうまく機能しますが、onDestroy() メソッドが呼び出されると、ウィジェットは機能しなくなります。
アプリが起動されていないにもかかわらず、ウィジェットが引き続き動作し、更新されるアプリをいくつか見てきました。少なくとも私の知る限りではありません。
これはどのように行われますか?アプリを実行しなくても、ウィジェットを実行して機能させたいと思います。これは可能ですか?
アップデート:
ここに私の MainActivity の onCreate() からのスニペットがあります
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final HandlerThread hTh = new HandlerThread("HT");
hTh.start();
final long oneScd = 1000;
final Handler handler = new Handler(hTh.getLooper());
Runnable updateDisplay = new Runnable(){
@Override
public void run() {
updateDisplay(GlobalSpeed._json);
}
};
Runnable eachSec = new Runnable() {
@Override
public void run() {
runOnUiThread(updateDisplay);
handler.postDelayed(this, oneScd);
}
};
handler.postDelayed(eachSec, oneScd);
ここに私の AppWidgetProvider コードがあります:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
try {
updateWidgetContent(context, appWidgetManager);
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed", e);
}
}
public void updateWidgetContent(final Context context, final AppWidgetManager appWidgetManager) {
Intent launchAppIntent = new Intent(context, SoleWidgetProvider.class);
service = PendingIntent.getActivity(context, 0, launchAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
hTh.start();
final long oneScd = 4000;
final Handler handler = new Handler(hTh.getLooper());
Runnable eachSec = new Runnable(){
@Override
public void run() {
String js;
js = GlobalSpeed._json;
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.sole_widget_layout);
remoteView.setTextViewText(R.id.wSpeed, js);//"Speed: " + String.valueOf(s.realSpeed())
ComponentName componentName = new ComponentName(context, SoleWidgetProvider.class);
appWidgetManager.updateAppWidget(componentName, remoteView);
handler.postDelayed(this, oneScd);
}
};
handler.postDelayed(eachSec, oneScd);
}
これは、シリアル通信からの応答を取得し、GlobalSpeed._json 値を設定する私のサービスです。
public class WidgetUpdateService extends Service {
public static final String DEBUG_TAG = "SoleWidgetService";
public static String json;
private SerialComunicator serComm;
private Context context;
@Override
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
Log.d(DEBUG_TAG, "Sevice Started");
serComm = new SerialComunicator(this);
buildUpdate();
}
private void buildUpdate(){
HandlerThread hTh = new HandlerThread("SHT");
final long oneScd = 4000;
hTh.start();
final Handler handler = new Handler(hTh.getLooper());
Runnable readSerial = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
GlobalSpeed._json = serComm.readDataFromSerial();
handler.postDelayed(this, oneScd);
}
};
handler.postDelayed(readSerial, oneScd);
}
public void displayToast(String msg){
Toast.makeText(this, serComm.readDataFromSerial(), Toast.LENGTH_SHORT);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
これで十分なコードであることを願っています。
これはすべて正常に機能し、ホーム画面に移動してアプリの onStop() メソッドが呼び出されると、機能し、ウィジェットは引き続き最新の状態になります。しかし、最近のアプリからアプリを削除し、onDestroy() メソッドが呼び出されると、ウィジェットの更新が停止します。
ウィジェットがアプリとは独立して機能することを望みます。これは可能ですか?アプリとは独立して実行されるように見えるウィジェットをいくつか知っています。