用a.apk啟動b.apk,並且b的圖標是在手機主界面上看不到的。
如何做才能達到既能被主程序啟動,又能隱藏起來呢?
其實很簡單,實現的方法是:在manifest的入口activity里面intent-filter中設置<data></data>元素。
步驟如下:
一、新建a,b兩個android項目(新建helloworld項目相似),在a項目中增加點擊事件(啟動按鈕來啟動b應用)。
二、在b應用中修改b manifest.xml中<intent-filter>...</intent-filter>的內容就可以隱藏b應用的圖標了。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rainwii.message" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.rainwii.message.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <!--這句可以注釋掉,也可以留着;注釋后,圖標可以隱藏但是安裝軟件后不直接啟動本應用--> <!-- <category android:name="android.intent.category.LAUNCHER" /> --> <!--data是啟動圖標隱藏應用的關鍵;注釋category了,android:scheme代碼就可以注釋掉。如果scheme的內容可以隨便寫,但是和要和a應用中的跳轉信息一致--> <data android:host="MainActivity" android:scheme="abc" /> </intent-filter> </activity> </application> </manifest>
現在安裝b應用的圖標在手機上已經看不見了。
三、a應用中button增加代碼來啟動b應用。
package com.rainwii.main; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.pm.PackageManager; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button startappbutton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startappbutton= (Button) findViewById(R.id.button1); startappbutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent();
//a應用啟動b應用 ComponentName cn = new ComponentName("com.rainwii.message", "com.rainwii.message.MainActivity"); intent.setComponent(cn); Uri uri = Uri.parse("abc");// 此處應與B程序中Data中標簽一致 intent.setData(uri); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
注釋:ComponentName(組件名稱)是用來打開其他應用程序中的Activity或服務的。
用法:
Intent it=new Intent(); it.setComponent(new ComponentName(String packageName,String activityName )); startActivity(it);
好了,已經實現了從一個應用啟動另一個主界面隱藏圖標的應用了。
==================================================
方法二:
[支付寶錢包],可以從支付寶直接跳轉到[去啊],如果沒有按照還提醒用戶是否安裝,有點炫酷哦,去啊的用戶量一下增多了
第一個App中
// 通過包名獲取要跳轉的app,創建intent對象 Intent intent = activity().getPackageManager() .getLaunchIntentForPackage("com.zsl.download"); // 這里如果intent為空,就說名沒有安裝要跳轉的應用嘛 if (intent != null) { // 這里跟Activity傳遞參數一樣的嘛,不要擔心怎么傳遞參數,還有接收參數也是跟Activity和Activity傳參數一樣 intent.putExtra("name", "鄭松嵐"); startActivity(intent); } else { // 沒有安裝要跳轉的app應用,提醒一下 ToastUtils.showLongToast(activity(), "沒安裝此APP"); }
第二個App中
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) { String name = bundle.getString("name"); if (name != null) { Toast.makeText(getApplicationContext(), "name:" + name, Toast.LENGTH_SHORT).show(); } }