Android后台运行
有Activity1、Activity2、Activity3三个Activity,每个Activity上面有一个按钮分别为B1、B2、B3.在AndroidManifest.xml中设置Activity1的intent-filter为
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
package com.alex.mock.qq;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MockQQService extends Service{
private NotificationManager mNM;
/**
* Class for clients to access. Because we know this service always
* runs in the same process as its clients, we don't need to deal with
* IPC.
*/
public class MockQQBinder extends Binder {
MockQQService getService() {
return MockQQService.this;
}
}
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
@Override
public void onDestroy() {
// Cancel the persistent notification.
mNM.cancel(R.string.hello);
// Tell the user we stopped.
Toast.makeText(this, "service stopped and notification will be removed.", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new MockQQBinder();
public void dismissNotification() {
mNM.cancel(R.string.hello);
}
/**
* Show a notification while this service is running.
*/
public void showNotification() {
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(android.R.drawable.btn_default, "",
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
Class<?> cls = getStartClassFromPrefences();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, cls), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, "mock qq title",
"mock qq message", contentIntent);
// Send the notification.
// We use a layout id because it is a unique number. We use it later to cancel.
mNM.notify(R.string.hello, notification);
}
private Class<?> getStartClassFromPrefences() {
SharedPreferences preference = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
String name = preference.getString(MockQQApplication.ACTIVITY_NAME, MockQQActivity.class.getSimpleName());
Class<?> cls = null;
if(name.equals(MockQQActivity.class.getSimpleName())) {
cls = MockQQActivity.class;
} else if (name.equals(ActivityB.class.getSimpleName())){
cls = ActivityB.class;
} else if (name.equals(ActivityC.class.getSimpleName())){
cls = ActivityC.class;
}
return cls;
}
}
package com.alex.mock.qq;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
public class MockQQApplication extends Application{
public final static String ACTIVITY_NAME = "activity_name";
private MockQQService mBoundService;
@Override
public void onCreate() {
super.onCreate();
bindService(new Intent(this,
MockQQService.class), mConnection, Context.BIND_AUTO_CREATE);
}
public MockQQService getMockQQService() {
return mBoundService;
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
mBoundService = ((MockQQService.MockQQBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
mBoundService = null;
}
};
}