1

我有一个活动和一个意图服务相互通信。Service 生成 100 个随机浮点数,并通过 Messenger 将它们发送到 Activity,延迟时间为 1 秒。它还使用进度百分比更新通知。

在手机旋转之前,这工作得很好。当手机旋转时,UI 不再更新(不显示最新的随机浮动)。我已经调试过,发现仍然在生成随机浮点数并发送到 Activity。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;
            }
      }
}
4

3 回答 3

1

您的 IntentService 类不需要 Handler 类...它继承了自己的处理程序方法,称为

protected void onHandleIntent(Intent arg0)
于 2013-01-11T06:52:13.633 回答
1

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

IntentService 没有一个简单的方法来解决这个问题——我可能会建议转移到绑定或普通启动服务,并在其 onCreate/onDestroy 调用中将 Activity 绑定/取消绑定到服务。

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

于 2013-02-20T22:59:01.327 回答
0

好的,所以这很有启发性。

事实证明,只要该 Service 存在,它的 onBind 方法就不会被第二次调用,即使您解除绑定并重新绑定也是如此。相反,如果您从 unbind 返回 true,则调用 rebind。

请注意,如果您等到服务停止随机发送,旋转它,然后启动它,它就可以正常工作。那是因为 onBind 在 onDestroy/onCreate 之后被调用。

我已经走得够远了,以至于我看到了发生了什么:我实际上并没有解决问题。不过,我认为,如果您实施 onRebind,您可能会使其工作。

顺便说一句,我会在这段代码中改变一些其他的东西:如果可能的话,我会让那些 Handlers 成为静态的。如果是我,我也会将 Activity 设为 ServiceConnection。

祝你好运!

编辑添加:

我受不了了。我检查了。唉,在 onRebind 中传递的意图不是在第二次调用 bindService 时发送的意图。我不知道如何使这项工作。

编辑添加

这仍然是一个 hack,但我认为它符合原始代码的意图:

https://github.com/bmeike/WeirdServer.git

于 2013-02-21T02:05:42.743 回答