编写:徐建祥(netpirate@gmail.com)
日期:2010/12/13
网址:http://www.anymobile.org
打开程序的入口有很多个:
shell 命令行运行;
Launcher待机界面执行;
状态通知栏运行;
桌面快捷方式运行;
软件中调用运行;
安装软件后执行“OPEN”运行!
前面几项,调用程序的代码如下(参考:com.android.Launcher/.Launcher.java):
- Intent intent = new Intent(this, TestActivity.class);
- intent.setAction(Intent.ACTION_MAIN);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
- this.startActivity(intent);
而安装软件后,点击“Open”调用的代码如下(参考:com.android.packageinstaller/.InstallAppDone.java):
- Intent intent = new Intent(this, TestActivity.class);
- this.startActivity(intent);
如果用户安装软件后立刻执行“Open”,运行程序后,按HOME键返回到后台,然后再通过别的几种方法运行程序,则会再起一个MAIN程序。这是因为Intent的处理机制是,先比较Activity,再比较Action、Flag、bnds。。。,前后两张方式的Action不一样,一个有LAUNCHER ACTION,一个没有,所以会认为是启动两个不同的INTENT。
目前只想到一个简单的处理方式:
程序入口MAIN程序:SplashActivity.java
程序原入口程序:LoginActivity.java
启动程序后,在状态通知栏上添加快捷通知,代码如下:
- package org.anymobile.test;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- public class SplashActivity extends Activity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
-
- this.showNotification(this.getBaseContext(), -1, "Test is Running!", "Test Start Up!");
-
- Intent intent = this.getIntent();
- if (intent.hasCategory(Intent.CATEGORY_LAUNCHER))
- {
- intent = new Intent(this, TestActivity.class);
- this.startActivity(intent);
- }
- else
- {
- intent = new Intent();
- ComponentName componentName = new ComponentName(this, SplashActivity.class);
- intent.setComponent(componentName);
-
-
- intent.setAction(Intent.ACTION_MAIN);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
-
- this.startActivity(intent);
- }
-
- this.finish();
- }
- public void showNotification(Context context,int iResIcon,String sNotifybar,String sNofitymsg)
- {
-
- NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
-
- CharSequence from = "Test";
- CharSequence message = sNofitymsg;
-
- Intent intent = new Intent();
- ComponentName componentName = new ComponentName(context, TestActivity.class);
- intent.setComponent(componentName);
-
- intent.setAction(Intent.ACTION_MAIN);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
-
- PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
- Notification notification =
- new Notification(iResIcon,sNotifybar, System.currentTimeMillis());
- notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
- notification.defaults =
- Notification.DEFAULT_LIGHTS;
- notification.setLatestEventInfo(context, from, message, contentIntent);
- nm.notify(R.string.app_name, notification);
- }
- @Override
- protected void onStart()
- {
-
- super.onStart();
- }
- @Override
- protected void onResume()
- {
-
- super.onResume();
- }
- @Override
- protected void onStop()
- {
-
- super.onStop();
- }
- @Override
- protected void onDestroy()
- {
-
- super.onDestroy();
- }
- }
- package org.anymobile.test;
- import android.app.Activity;
-
- import android.content.Intent;
- import android.graphics.LightingColorFilter;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class TestActivity extends Activity
- {
- private static final int ADD_ID = 0;
- private static final int DEL_ID = 1;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- Intent intent = this.getIntent();
- if (! intent.hasCategory(Intent.CATEGORY_LAUNCHER))
- {
- this.getIntent().addCategory(Intent.CATEGORY_LAUNCHER);
- }
- this.getWindow().addFlags(CONTEXT_IGNORE_SECURITY);
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- Button button = (Button) this.findViewById(R.id.btn_menu);
- button.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Intent intent = new Intent(TestActivity.this, Test2Activity.class);
- TestActivity.this.startActivity(intent);
- TestActivity.this.finish();
- }
- });
-
- button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
- }
- @Override
- protected void onRestoreInstanceState(Bundle savedInstanceState) {
-
- super.onRestoreInstanceState(savedInstanceState);
- }
- @Override
- protected void onSaveInstanceState(Bundle outState) {
-
- super.onSaveInstanceState(outState);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- menu.add(0, ADD_ID, 0, "ADD");
- menu.add(0, DEL_ID, 0, "DEL");
-
- return super.onCreateOptionsMenu(menu);
- }
-
-
- }
- package org.anymobile.test;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.LightingColorFilter;
- import android.os.Bundle;
- import android.text.ClipboardManager;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class Test2Activity extends Activity
- {
- private static final int ADD_ID = 0;
- private static final int DEL_ID = 1;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- ClipboardManager clip = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
-
- Button button = (Button) this.findViewById(R.id.btn_menu);
- button.setText("22222222");
- button.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- Test2Activity.this.openOptionsMenu();
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- });
-
- button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- menu.add(0, ADD_ID, 0, "ADD2");
- menu.add(0, DEL_ID, 0, "DEL2");
-
- return super.onCreateOptionsMenu(menu);
- }
-
-
- }
测试流程:
a、软件本地安装;
b、安装后执行“Open”;
c、运行软件会从SplashActivity添加NOTICIFICATION TestActivity;
d、跳转至SplashActivity(附带LAUNCHER ACTION);
e、再跳转至TestActivity;
f、点击BUTTON,跳转至Test2Activity;
g、下拉状态通知栏,点击程序通知,可以正常运行程序。
TODO: 安装软件后执行OPEN后,从待机界面运行程序,还是会启动两个一样的ACTIVITY,这个问题没有解决!:(
posted on 2010-12-14 23:11
Xu Jianxiang 阅读(2046)
评论(2) 编辑 收藏 所属分类:
Android