編寫:徐建祥(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);<br />intent.setAction(Intent.ACTION_MAIN);<br />intent.addCategory(Intent.CATEGORY_LAUNCHER);<br />intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br />intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);<br />this.startActivity(intent);
而安裝軟體後,點擊“Open”調用的代碼如下(參考:com.android.packageinstaller/.InstallAppDone.java):
Intent intent = new Intent(this, TestActivity.class);<br />this.startActivity(intent);
如果使用者安裝軟體後立刻執行“Open”,運行程式後,按HOME鍵返回到後台,然後再通過別的幾種方法運行程式,則會再起一個MAIN程式。這是因為Intent的處理機制是,先比較Activity,再比較Action、Flag、bnds。。。,前後兩張方式的Action不一樣,一個有LAUNCHER ACTION,一個沒有,所以會認為是啟動兩個不同的INTENT。
目前只想到一個簡單的處理方式:
程式入口MAIN程式:SplashActivity.java
程式原入口程式:LoginActivity.java
啟動程式後,在狀態通知欄上添加快捷通知,代碼如下:
package org.anymobile.test;<br />import android.app.Activity;<br />import android.app.Notification;<br />import android.app.NotificationManager;<br />import android.app.PendingIntent;<br />import android.content.ComponentName;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.os.Bundle;<br />public class SplashActivity extends Activity<br />{<br />@Override<br />protected void onCreate(Bundle savedInstanceState)<br />{<br />super.onCreate(savedInstanceState);</p><p>this.showNotification(this.getBaseContext(), -1, "Test is Running!", "Test Start Up!");</p><p>Intent intent = this.getIntent();<br />if (intent.hasCategory(Intent.CATEGORY_LAUNCHER))<br />{<br />intent = new Intent(this, TestActivity.class);<br />this.startActivity(intent);<br />}<br />else<br />{<br />intent = new Intent();<br />ComponentName componentName = new ComponentName(this, SplashActivity.class);<br />intent.setComponent(componentName);<br />//intent = new Intent(this, SplashActivity.class);</p><p>intent.setAction(Intent.ACTION_MAIN);<br />intent.addCategory(Intent.CATEGORY_LAUNCHER);<br />intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br />intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);</p><p>this.startActivity(intent);<br />}</p><p>this.finish();<br />}<br /> public void showNotification(Context context,int iResIcon,String sNotifybar,String sNofitymsg)<br /> {<br /> // look up the notification manager service<br /> NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);<br /> // The details of our fake message<br /> CharSequence from = "Test";<br /> CharSequence message = sNofitymsg;</p><p>Intent intent = new Intent();<br />ComponentName componentName = new ComponentName(context, TestActivity.class);<br />intent.setComponent(componentName);</p><p>intent.setAction(Intent.ACTION_MAIN);<br />intent.addCategory(Intent.CATEGORY_LAUNCHER);<br />intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br />intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);</p><p> PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);<br /> Notification notification =<br /> new Notification(iResIcon,sNotifybar, System.currentTimeMillis());<br /> notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;<br /> notification.defaults =<br /> /*Notification.DEFAULT_SOUND|*/Notification.DEFAULT_LIGHTS;<br /> notification.setLatestEventInfo(context, from, message, contentIntent);<br /> nm.notify(R.string.app_name, notification);<br /> }<br />@Override<br />protected void onStart()<br />{<br />// TODO Auto-generated method stub<br />super.onStart();<br />}<br />@Override<br />protected void onResume()<br />{<br />// TODO Auto-generated method stub<br />super.onResume();<br />}<br />@Override<br />protected void onStop()<br />{<br />// TODO Auto-generated method stub<br />super.onStop();<br />}<br />@Override<br />protected void onDestroy()<br />{<br />// TODO Auto-generated method stub<br />super.onDestroy();<br />}<br />}<br />
package org.anymobile.test;<br />import android.app.Activity;<br />//import android.content.ComponentName;<br />import android.content.Intent;<br />import android.graphics.LightingColorFilter;<br />import android.os.Bundle;<br />import android.view.Menu;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />public class TestActivity extends Activity<br />{<br />private static final int ADD_ID = 0;<br />private static final int DEL_ID = 1;</p><p> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState)<br /> {<br />Intent intent = this.getIntent();<br />if (! intent.hasCategory(Intent.CATEGORY_LAUNCHER))<br />{<br />this.getIntent().addCategory(Intent.CATEGORY_LAUNCHER);<br />}<br />this.getWindow().addFlags(CONTEXT_IGNORE_SECURITY);</p><p> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> Button button = (Button) this.findViewById(R.id.btn_menu);<br /> button.setOnClickListener(new OnClickListener()<br /> {<br /> public void onClick(View v)<br /> {<br />// TestActivity.this.openOptionsMenu();</p><p>// String packName = "com.longcheer.imm";<br />// String className = packName + ".activitys.LoginActivity";<br />//<br />// Intent intent = new Intent();<br />// ComponentName componentName = new ComponentName(packName, className);<br />// intent.setComponent(componentName);<br />//<br />// intent.setAction("android.intent.action.MAIN");<br />// intent.addCategory("android.intent.category.LAUNCHER");<br />// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br />// intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);<br />//<br />// TestActivity.this.startActivity(intent);</p><p> Intent intent = new Intent(TestActivity.this, Test2Activity.class);<br /> TestActivity.this.startActivity(intent);<br /> TestActivity.this.finish();<br /> }<br /> });<br />// button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);<br /> button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));<br /> }<br />@Override<br />protected void onRestoreInstanceState(Bundle savedInstanceState) {<br />// TODO Auto-generated method stub<br />super.onRestoreInstanceState(savedInstanceState);<br />}<br />@Override<br />protected void onSaveInstanceState(Bundle outState) {<br />// TODO Auto-generated method stub<br />super.onSaveInstanceState(outState);<br />}<br />@Override<br />public boolean onCreateOptionsMenu(Menu menu)<br />{<br />menu.add(0, ADD_ID, 0, "ADD");<br />menu.add(0, DEL_ID, 0, "DEL");</p><p>return super.onCreateOptionsMenu(menu);<br />}</p><p>}
package org.anymobile.test;<br />import android.app.Activity;<br />import android.content.ComponentName;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.graphics.LightingColorFilter;<br />import android.os.Bundle;<br />import android.text.ClipboardManager;<br />import android.view.Menu;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />public class Test2Activity extends Activity<br />{<br />private static final int ADD_ID = 0;<br />private static final int DEL_ID = 1;</p><p> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState)<br /> {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> ClipboardManager clip = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);</p><p> Button button = (Button) this.findViewById(R.id.btn_menu);<br /> button.setText("22222222");<br /> button.setOnClickListener(new OnClickListener()<br /> {<br /> public void onClick(View v)<br /> {<br /> Test2Activity.this.openOptionsMenu();<br />// String packName = "com.longcheer.imm";<br />// String className = packName + ".activitys.LoginActivity";<br />//<br />// Intent intent = new Intent();<br />// ComponentName componentName = new ComponentName(packName, className);<br />// intent.setComponent(componentName);<br />//<br />// intent.setAction("android.intent.action.MAIN");<br />// intent.addCategory("android.intent.category.LAUNCHER");<br />// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br />// intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);<br />//<br />// Test2Activity.this.startActivity(intent);<br /> }<br /> });<br />// button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);<br /> button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));<br /> }<br />@Override<br />public boolean onCreateOptionsMenu(Menu menu)<br />{<br />menu.add(0, ADD_ID, 0, "ADD2");<br />menu.add(0, DEL_ID, 0, "DEL2");</p><p>return super.onCreateOptionsMenu(menu);<br />}</p><p>}
測試流程:
a、軟體本地安裝;
b、安裝後執行“Open”;
c、運行軟體會從SplashActivity添加NOTICIFICATION TestActivity;
d、跳轉至SplashActivity(附帶LAUNCHER ACTION);
e、再跳轉至TestActivity;
f、點擊BUTTON,跳轉至Test2Activity;
g、下拉狀態通知欄,點擊程式通知,可以正常運行程式。
TODO: 安裝軟體後執行OPEN後,從待機介面運行程式,還是會啟動兩個一樣的ACTIVITY,這個問題沒有解決!:(