在我们的APP中用于与其他APP交互
implicit intent 在声明时并不需要指定跳转的activity的名称,而是声明一个要执行的动作action,action中可以携带数据,也可以不携带,根据需求而定
比如说,当我们需要在我们的APP中呼叫某一个电话
package net.oschina.git.zhaikun.androiddeveloped.activitys; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import net.oschina.git.zhaikun.androiddeveloped.R; import net.oschina.git.zhaikun.androiddeveloped.utils.MyTextUtils; import java.util.List; /** * Created by zhaikun68 on 2017/8/3. * <p> * 隐式意图 */ public class ImplicitIntentActivity extends AppCompatActivity implements View.OnClickListener { private static final String CALL_10086 = "10086";//呼叫10086 private static final String LOG_TAG = "ImplicitIntentActivity"; private static final String MAP_URI = "geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California"; private Button callPhoneBtn;//拨打电话 private Button mapBtn;//地图 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_implicit_intent); initView(); } /** * 组件初始化 */ private void initView() { callPhoneBtn = (Button) findViewById(R.id.btn_call_phone); mapBtn = (Button) findViewById(R.id.btn_map); callPhoneBtn.setOnClickListener(this); mapBtn.setOnClickListener(this); } /** * Called when a view has been clicked. * * @param v The view that was clicked. */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_call_phone: callPhone(CALL_10086);//呼叫10086 break; case R.id.btn_map: openMap(MAP_URI);//地图 break; default: break; } } /** * 电话呼叫 * * @param phoneNumber 电话号码 */ private void callPhone(String phoneNumber) { if (MyTextUtils.isTrimEmpty(phoneNumber)) Log.e(LOG_TAG, getString(R.string.parameter_is_empty)); else { Intent callPhoneIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)); //在intent触发之前,验证是否有APP可以接受当前的intent,否则会引起APP crash(崩溃) PackageManager packageManager = getPackageManager(); List<ResolveInfo> acceptActivityList = packageManager.queryIntentActivities(callPhoneIntent, 0); if (acceptActivityList == null || acceptActivityList.size() == 0) Log.e(LOG_TAG, "没有APP接受当前intent"); else startActivity(callPhoneIntent);//intent发送给系统后,如果只有一个APP可以接收当前意图,那么会直接打开接收的APP,如果有多个APP可以接收当前intent,那么系统会显示一个Dialog,用户可以选择使用哪个APP开启当前intent } } /** * 开启地图 * * @param mapUri 地图链接地址 */ private void openMap(String mapUri) { if (MyTextUtils.isTrimEmpty(mapUri)) Log.e(LOG_TAG, getString(R.string.parameter_is_empty)); else { Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mapUri)); PackageManager packageManager = getPackageManager(); List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(mapIntent, 0); if (resolveInfos == null || resolveInfos.size() == 0) Log.e(LOG_TAG, "open map failed"); else startActivity(mapIntent); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_call_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="呼叫10086"/> <Button android:id="@+id/btn_map" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="地图"/> </LinearLayout>