Intent之实现功能导航

来源:互联网 发布:淘宝子账号可以开店吗 编辑:程序博客网 时间:2024/05/02 06:10

Intent之实现功能导航

其实很多诸如跳转到通讯录,拨打电话等功能,Action上都有明确提供了。这里主要想说的是如何跳转到一个第三方的应用,即跳转到微信之类的。下面实例如何跳转到微信

           Intent intent = new Intent();        //"com.tencent.mm""com.tencent.mm.ui.LauncherUI"可以通过logcat获取        ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        intent.setComponent(componentName);        startActivity(intent);

那么,归根到底,实现的步骤就是:

  1. 创建一个Intent对象:Intent intent = new Intent()

  2. 创建一个ComponentName对象: ComponentName componentName = new ComponentName(”包名”,”Activity类名”)

  3. 实现intent的setComponent方法:intent.setComponent(componentName)

  4. 开始跳转:startActivity(intent)

下面直接看代码,先看布局文件代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal">        <Button            android:id="@+id/btn_wifi"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:text="无线" />        <Button            android:id="@+id/btn_call"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:text="电话" />        <Button            android:id="@+id/btn_mm"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:text="微信" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal">        <Button            android:id="@+id/btn_music"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:text="音乐" />        <Button            android:id="@+id/btn_contacts"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:text="联系人" />        <Button            android:id="@+id/btn_sms"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:text="短信" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal">        <Button            android:id="@+id/btn_calendar"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:text="日历" />        <Button            android:id="@+id/btn_news"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:text="新闻" />        <Button            android:id="@+id/btn_weather"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:text="天气" />    </LinearLayout></LinearLayout>

Activity代码

package com.shake.myaction;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.net.Uri;import android.os.Build;import android.provider.CalendarContract;import android.provider.Contacts;import android.provider.ContactsContract;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity implements View.OnClickListener {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViews();    }    private void findViews() {        findViewById(R.id.btn_wifi).setOnClickListener(this);        findViewById(R.id.btn_call).setOnClickListener(this);        findViewById(R.id.btn_mm).setOnClickListener(this);        findViewById(R.id.btn_music).setOnClickListener(this);        findViewById(R.id.btn_contacts).setOnClickListener(this);        findViewById(R.id.btn_sms).setOnClickListener(this);        findViewById(R.id.btn_calendar).setOnClickListener(this);        findViewById(R.id.btn_news).setOnClickListener(this);        findViewById(R.id.btn_weather).setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            //打开wifi设置            case R.id.btn_wifi:                intentWifi();                break;            //打开通话记录            case R.id.btn_call:                intentCall();                break;            //打开微信            case R.id.btn_mm:                intentMm();                break;            //打开音乐播放器            case R.id.btn_music:                intentMusic();                break;            //打开通讯录            case R.id.btn_contacts:                intentContacts();                break;            case R.id.btn_sms:                intentSms();                break;            //打开日历            case R.id.btn_calendar:                intentCalendar();                break;            //打开网易新闻            case R.id.btn_news:                intentNews();                break;            //打开天气            case R.id.btn_weather:                intentWeather();                break;        }    }    /**     * 打开天气     */    private void intentWeather() {        Intent intent = new Intent();        ComponentName cn = new ComponentName("com.miui.weather2",                "com.miui.weather2.ActivityWeatherCycle");        intent.setComponent(cn);        startActivity(intent);    }    /**     * 打开网易新闻     */    private void intentNews() {        Intent intent = new Intent();        ComponentName cn = new ComponentName("com.netease.newsreader.activity",                "com.netease.nr.biz.ad.AdActivity");        intent.setComponent(cn);       startActivity(intent);    }    /**     * 打开日历     */    private void intentCalendar() {        Intent intent =new Intent();        ComponentName componentName =new ComponentName("com.android.calendar","com.android.calendar.LaunchActivity");        intent.setComponent(componentName);        startActivity(intent);    }    /**     * 打开短信     */    private void intentSms() {        Intent intent = new Intent();        intent.setClassName("com.android.mms", "com.android.mms.ui.ConversationList");        startActivity(intent);    }    /**     * 打开通讯录     */    private void intentContacts() {        Intent intent =new Intent();        intent.setAction(Intent.ACTION_VIEW);        intent.setData(Contacts.People.CONTENT_URI);        startActivity(intent);    }    /**     * 打开音乐播放器     */    private void intentMusic() {        Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");        startActivity(intent);    }    /**     * 打开微信     */    private void intentMm() {        Intent intent = new Intent();        //"com.tencent.mm"和"com.tencent.mm.ui.LauncherUI"可以通过logcat获取        ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        intent.setComponent(componentName);        startActivity(intent);    }    /**     * 打开通话记录     */    private void intentCall() {        Intent intent = new Intent();        intent.setAction("android.intent.action.CALL_BUTTON");        startActivity(intent);    }    /**     * wifi设置     */    private void intentWifi() {        Intent intent = new Intent();        intent.setAction("android.net.wifi.PICK_WIFI_NETWORK");        startActivity(intent);    }}

效果:在手机上测试过全部都可以实现,但不同手机效果也不一样的
这里写图片描述

0 0