有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效果,如何实现?求高手指点??
<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效果,如何实现?求高手指点??
解决方案 »
- android 彩色图片转换成黑白的图片(灰色)
- NDK下调用JAVA层的类,老是报signal 11 (SIGSEGV),code 1 (SEGV_MAPERR), fault addr fffffff4
- 求问dedexer反编译dex文件后生成的ddx文件怎么变为java或者smali怎么用
- View的LongClick事件不要震动
- 能否在子类中改变父类用到的变量或者数据源?
- [求助]關於GPS座標顯示方法(連結資料庫)
- android连接蓝牙键盘的问题
- Android在电脑上实现文件管理, 是个什么原理?
- 字符串 问题 求帮忙
- 关于autoCompleteTextView显示不了
- 如何设置autoCompleteTextView的提示个数
- 如何自定义点击hyperlink之后的响应事件
重载activity2中的boolean onKeyDown (int keyCode, KeyEvent event)方法。让back键实现home键的操作。
在“Activity2显示时切换到桌面,再点通知图标后切换会Activity2,在Activity3显示时切换到桌面,再点通知图标后切换会Activity3。仿android版QQ效果,如何实现?求高手指点??”
在minifest中设置ActivityX的launchMode属性值为singleTask,并在ActivityX(X:1-3)切换到桌面的时候调用PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ActivityX.class), 0);
notification.setLatestEventInfo(this, getText(R.string.alarm_service_label),
text, contentIntent);
mNM.notify(R.string.xxx, notification);
第二种方法:在Activity1中截获back按键消息,然后把程序隐藏,不过这样就到了桌面。
我是想要实现QQ那种效果,按BACK后程序切换到后台,但点击通知图标或lanucher中的图标都会切换回之前的界面,就好像只允许一个程序的实例运行。
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;
}
};}
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;public abstract class BaseActivity extends Activity{ MockQQApplication mApplication; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
protected void onResume() {
super.onResume();
mApplication = (MockQQApplication) getApplicationContext();
MockQQService boundService = mApplication.getMockQQService();
if(boundService != null) {
boundService.dismissNotification();
}
} @Override
public void onBackPressed() {
SharedPreferences preference = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
Editor editor = preference.edit();
editor.putString(MockQQApplication.ACTIVITY_NAME, getActivityName());
editor.commit();
mApplication.getMockQQService().showNotification();
finish();
} abstract public String getActivityName();
}
Activity Apackage com.alex.mock.qq;import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;public class MockQQActivity extends BaseActivity { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
startActivityFromPreferencesIfNeed();
TextView activityName = (TextView) findViewById(R.id.activity_name);
activityName.setText("activity A");
} private void startActivityFromPreferencesIfNeed() {
SharedPreferences preference = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
String name = preference.getString(MockQQApplication.ACTIVITY_NAME, MockQQActivity.class.getSimpleName());
Class<?> cls = null;
if (name.equals(ActivityB.class.getSimpleName())){
cls = ActivityB.class;
} else if (name.equals(ActivityC.class.getSimpleName())){
cls = ActivityC.class;
}
if(cls != null) {
Intent intent = new Intent(this, cls);
startActivity(intent);
finish();
}
} public void onButtonClick(View v) {
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
finish();
} @Override
public String getActivityName() {
return getLocalClassName();
}
}package com.alex.mock.qq;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;public class ActivityB extends BaseActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
TextView activityName = (TextView) findViewById(R.id.activity_name);
activityName.setText("activity B");
} public void onButtonClick(View v) {
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
finish();
} @Override
public String getActivityName() {
return getLocalClassName();
}
}package com.alex.mock.qq;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;public class ActivityC extends BaseActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
TextView activityName = (TextView) findViewById(R.id.activity_name);
activityName.setText("activity C");
} public void onButtonClick(View v) {
Intent intent = new Intent(this, MockQQActivity.class);
startActivity(intent);
finish();
} @Override
public String getActivityName() {
return getLocalClassName();
}
}
Manifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alex.mock.qq"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" /> <application android:name="MockQQApplication"
android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MockQQActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name=".ActivityB"
android:label="@string/app_name">
</activity> <activity android:name=".ActivityC"
android:label="@string/app_name">
</activity> <service android:name=".MockQQService" />
</application>
</manifest>
假如程序的进程被其他程序杀死,那么sharedpreferences的值是否会残留?导致下次启动时启动了错误的Activity(我是想要程序真实退出后,再次启动,重新从第一个Activity开始运行)?本来是想加个退出菜单,在点击时清空sharedpreferences的值,以便下次重新从第一个Activity开始,但这样也不能保证在被其他程序杀掉时能清空sharedpreferences
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/activity_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity"
android:onClick="onButtonClick"/>
</LinearLayout>