我有一个Activity和一个Intent Service相互通信。该服务会生成100个随机浮动,并通过Messenger将其延迟1秒发送到活动。它还会更新进度为%的通知。

旋转手机之前,这可以很好地工作。旋转手机时,UI不再更新(不显示最新的随机浮动)。我已经调试,发现随机浮点数仍在生成并发送到Activity。该活动甚至在调用tv.setText(“ New random number:” + random);但没有显示新的随机数。

有任何想法吗?

我的服务

package ie.cathalcoffey.android.test;

import java.util.Random;

import com.jakewharton.notificationcompat2.NotificationCompat2;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

public class MyService extends IntentService
{
    boolean stop = false;

    // Used to receive messages from the Activity
    final Messenger inMessenger = new Messenger(new IncomingHandler());

    // Use to send message to the Activity
    private Messenger outMessenger;

    class IncomingHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg)
        {
            Bundle data = msg.getData();
            stop = data.getBoolean("stop", true);
        }
     }

    public MyService ()
    {
          super("MyServerOrWhatever");
    }

    public MyService(String name)
    {
        super(name);
    }

    NotificationManager mNotificationManager;
    Notification notification;
    Random r;

    @Override
    public IBinder onBind(Intent intent)
    {
        Bundle extras = intent.getExtras();

        if (extras != null)
        {
            outMessenger = (Messenger) extras.get("messenger");
        }

        return inMessenger.getBinder();
    }

    @Override
    public void onCreate()
    {
        super.onCreate();

        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        r = new Random();

        mNotificationManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);

        NotificationCompat2.Builder mBuilder =
            new NotificationCompat2.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setProgress(100, 0, false)
            .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), TestServiceActivity.class), PendingIntent.FLAG_ONE_SHOT));

        notification = mBuilder.build();

        startForeground( 42, notification );
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();

        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startid)
    {
        super.onStart(intent, startid);

        stop = false;

        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        for(int i = 1; i < 100; i++)
        {
            if(stop)
            {
                break;
            }

            notification.contentView.setProgressBar(android.R.id.progress, 100, i, false);
            mNotificationManager.notify(42, notification);

            try
            {
                Message backMsg = Message.obtain();
                Bundle bundle = new Bundle();
                bundle.putFloat("randomFloat", r.nextFloat());
                backMsg.setData(bundle);

                outMessenger.send(backMsg);
                Thread.sleep(1000);
            }

            catch (Exception e)
            {

            }
        }
    }
}


我的活动

package ie.cathalcoffey.android.test;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TestServiceActivity extends Activity implements OnClickListener
{
      Messenger messenger = null;

      private Handler handler = new Handler()
      {
        public void handleMessage(Message message)
        {
            Bundle data = message.getData();
            float random = data.getFloat("randomFloat");

            TextView tv = (TextView)findViewById(R.id.textView1);
            tv.setText("New random number: " + random);
        }
      };

      private ServiceConnection conn = new ServiceConnection() {

            public void onServiceConnected(ComponentName className, IBinder binder) {
              messenger = new Messenger(binder);

            }

            public void onServiceDisconnected(ComponentName className) {
              messenger = null;
            }
          };

      private static final String TAG = "ServicesDemo";
      Button buttonStart, buttonStop;

      @Override
      protected void onResume()
      {
            // TODO Auto-generated method stub
            super.onResume();

            Intent intent = null;
            intent = new Intent(this, MyService.class);
            // Create a new Messenger for the communication back
            // From the Service to the Activity
            Messenger messenger = new Messenger(handler);
            intent.putExtra("messenger", messenger);

            bindService(intent, conn, Context.BIND_AUTO_CREATE);
      }

      @Override
      protected void onPause()
      {
            super.onPause();

            unbindService(conn);
      }

      @Override
      public void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            buttonStart = (Button) findViewById(R.id.buttonStart);
            buttonStop = (Button) findViewById(R.id.buttonStop);

            buttonStart.setOnClickListener(this);
            buttonStop.setOnClickListener(this);
      }

      @Override
      public void onClick(View src)
      {
            switch (src.getId())
            {
                case R.id.buttonStart:

                  startService(new Intent(this, MyService.class));
                  break;

                case R.id.buttonStop:
                  stopService(new Intent(this, MyService.class));
                  Message msg = Message.obtain();

                  try
                  {
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("stop", true);
                    msg.setData(bundle);
                    messenger.send(msg);
                  }

                  catch (RemoteException e) {
                    e.printStackTrace();
                  }
                  break;
            }
      }
}

最佳答案

当屏幕旋转时,启动IntentService的原始Activity被终止/重新创建。因此,该服务正忙于将浮点数发送到旧的僵尸活动,而不是发送到屏幕上旋转的新实例。那也是僵尸的内存泄漏。

IntentService并没有解决此问题的简便方法-我可能建议转移到Bound或Vanilla Started Service,并在其onCreate / onDestroy调用中将Activity绑定/取消绑定到该服务。

如果不需要Gingerbread支持,则Fragment允许您调用setRetainInstance使自己保持活动状态,即使周围的Activity被杀死/重新创建也是如此。仅需进行一些GUI重构,这似乎是一个更容易的更改。

10-05 21:01
查看更多