首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android后台老板运行

2013-06-19 
Android后台运行有Activity1、Activity2、Activity3三个Activity,每个Activity上面有一个按钮分别为B1、B2、B3

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>

现在要在Activity1启动后,点击按钮B1跳转到Activity2,同时Activity1调用finish()结束。此时要按BACK键返回,让程序后台运行,在通知栏显示图标。之后在程序退出前,如何让再次点击应用程序列表中的图标时自动跳到Activity2 (即保存程序状态,第一次运行后点击B1由Activity1跳到Activity2,然后按BACK切换回桌面,之后再点程序图标让程序自动切换回按BACK之前的状态)? 在Activity2显示时切换到桌面,再点通知图标后切换会Activity2,在Activity3显示时切换到桌面,再点通知图标后切换会Activity3。仿android版QQ效果,如何实现?求高手指点??
[解决办法]
代码上传不上来,只有贴在这了。


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;
        }
    };

}


热点排行