Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
onDestroy()之后的Android服务UI更新_Android_User Interface_Service_Android Intent - Fatal编程技术网

onDestroy()之后的Android服务UI更新

onDestroy()之后的Android服务UI更新,android,user-interface,service,android-intent,Android,User Interface,Service,Android Intent,我正在学习如何正确使用服务,并遇到了一个问题。我只有一个测试应用程序,它将启动一个服务来增加一个整数。我能够使用messenger以整数增量更新UI。此外,我能够将原始活动的启动模式设置为singleTask,这允许我使用服务中的挂起意图返回到主活动的实例 我面临的问题是,在旧活动被销毁(如作为屏幕旋转、退出应用程序等的一部分)后,如何将新活动(实际上我专注于进入新的UI视图)重新附加到我的服务? 在屏幕旋转或活动被破坏后会发生什么?重新打开时的UI与应用程序启动时的方式相同,但我可以看到我的服

我正在学习如何正确使用服务,并遇到了一个问题。我只有一个测试应用程序,它将启动一个服务来增加一个整数。我能够使用messenger以整数增量更新UI。此外,我能够将原始活动的启动模式设置为singleTask,这允许我使用服务中的挂起意图返回到主活动的实例


我面临的问题是,在旧活动被销毁(如作为屏幕旋转、退出应用程序等的一部分)后,如何将新活动(实际上我专注于进入新的UI视图)重新附加到我的服务?

在屏幕旋转或活动被破坏后会发生什么?重新打开时的UI与应用程序启动时的方式相同,但我可以看到我的服务仍在运行

以下是我的服务代码:

public class BackgroundService extends Service {

private static final String TAG = "BackgroundService";
private NotificationManager notificationMgr;
int counter;

// use regular thread
private ThreadGroup myThreads = new ThreadGroup("ServiceWorker");

public void onCreate() {
    super.onCreate();
    notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    displayNotificationMessage("Background Service is running");

};

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    super.onStartCommand(intent, flags, startId);

    int counter = intent.getExtras().getInt("counter");
    Messenger msg = (Messenger) intent.getExtras().get("msg_JIB");
    new Thread(myThreads, new ServiceWorker(counter, msg), "BackgroundService")
        .start();

    return START_NOT_STICKY;
}


 class ServiceWorker implements Runnable
    {
        int counter = -1;
        private Messenger msg1 = null;
        public ServiceWorker(int counter, Messenger msg) {
            this.counter = counter;
            this.msg1 = msg;
        }

        public void run() {
            final String TAG2 = "ServiceWorker:" + Thread.currentThread().getId();
            // do background processing here...
            try {
                while (counter<100){


                Message message = Message.obtain();
                message.arg1=counter;
                msg1.send(message);
                counter = counter +1;
                Thread.sleep(5000);
                }

            } catch (Throwable e) {

                Log.v(TAG2, "... sleep interrupted");
            }
        }
    }


private void displayNotificationMessage(String message) {
    Notification notification = new Notification(R.drawable.emo_im_winking,
            message, System.currentTimeMillis());


    Intent i = new Intent(this, ProDroid_Android_CH11_Local_ServiceActivity.class);
    //i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            i, 0);


    notification.setLatestEventInfo(this, TAG, message, contentIntent);
    notification.flags |= Notification.FLAG_NO_CLEAR;
    notificationMgr.notify(0, notification);

}

   @Override
    public void onDestroy()
    {
        Log.v(TAG, "in onDestroy(). Interrupting threads and cancelling notifications");
        myThreads.interrupt();
        notificationMgr.cancelAll();
        super.onDestroy();
    }


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

任何帮助都将不胜感激


Thx

您应该设置一个共享处理程序,其中当前活动将自己注册为处理程序的回调

看到这个帖子了吗

我遇到的问题是,在旧活动被销毁后(例如作为屏幕旋转、退出应用程序等的一部分),我如何将新活动重新附加到我的服务上?-这就是我的问题。你知道答案海报上的“别忘了在AndroidManifest.xml中设置我的应用程序名”是什么意思吗。我试图让它工作,但应用程序没有进入UI。在清单文件中,您可以设置自定义的
应用程序
(即,您可以将
应用程序
子类化以创建自己的应用程序)。要让应用程序使用它,您需要在manifest.xml中定义它。例如,
public class ProDroid_Android_CH11_Local_ServiceActivity extends Activity {

private static final String TAG = "MainActivity";
private int counter = 1;

Handler handler = new Handler() {
    @Override
    public void handleMessage(android.os.Message msg) {

        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setText("Count: " + msg.arg1);

    };

};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

@Override
protected void onNewIntent(Intent intent) {

    super.onNewIntent(intent);
}

public void doClick(View view) {
    switch (view.getId()) {
    case R.id.startBtn:
        Log.v(TAG, "Starting service... counter = " + counter);
        Intent intent = new Intent(
                ProDroid_Android_CH11_Local_ServiceActivity.this,
                BackgroundService.class);
        intent.putExtra("counter", counter);
        intent.putExtra("msg_JIB", new Messenger(handler));
        startService(intent);

        break;
    case R.id.stopBtn:
        stopService();
    }
}

private void stopService() {
    Log.v(TAG, "Stopping service...");
    if (stopService(new Intent(
            ProDroid_Android_CH11_Local_ServiceActivity.this,
            BackgroundService.class)))
        Log.v(TAG, "stopService was successful");
    else
        Log.v(TAG, "stopService was unsuccessful");
}

@Override
public void onDestroy() {
    //stopService();
    super.onDestroy();
}