Activity と Intent Service が互いに通信しています。Service は 100 個のランダムな float を生成し、Messenger を介して 1 秒の遅延で Activity に送信します。また、% の進行状況で通知を更新します。
これは、電話が回転するまで美しく機能しています。携帯電話を回転させると、UI が更新されなくなりました (最新のランダム フロートは表示されません)。デバッグしたところ、ランダムなフロートがまだ生成されてアクティビティに送信されていることがわかりました。アクティビティは 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;
}
}
}