• android之隐式intent调用


    直接上代码

    MainActivity.java

     1 package com.example.test1;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.net.Uri;
     6 import android.os.Bundle;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.widget.Button;
    10 
    11 public class MainActivity extends Activity {
    12 
    13     @Override
    14     protected void onCreate(Bundle savedInstanceState) {
    15         super.onCreate(savedInstanceState);
    16         setContentView(R.layout.activity_main);
    17     
    18         Button btnButton = (Button) findViewById(R.id.button1);
    19         
    20         btnButton.setOnClickListener(new OnClickListener() {
    21             
    22             @Override
    23             public void onClick(View v) {
    24                 // TODO Auto-generated method stub
    25                 Intent intent = new Intent();
    26                 intent.setAction("aa.bb.cc.dd.ee.ff");
    27                 
    28                 startActivity(intent);
    29             }
    30         });
    31     }
    32 }

    SecondActivity.java

     1 package com.example.test1;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 import android.widget.TextView;
     7 
     8 public class SecondActivity extends Activity {
     9 
    10     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         
    13         super.onCreate(savedInstanceState);
    14         
    15         setContentView(R.layout.second_main);
    16 
    17         Intent intent = getIntent();
    18         
    19         TextView textView = (TextView) findViewById(R.id.textView1);
    20         textView.setText(intent.getAction());
    21     }
    22 }

    activity_main.xml

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" >
    10 
    11     <Button
    12         android:id="@+id/button1"
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:layout_alignParentTop="true"
    16         android:layout_centerHorizontal="true"
    17         android:layout_marginTop="124dp"
    18         android:text="跳转" />
    19 
    20 </RelativeLayout>

    second_main.xml

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     >
     6 
     7     <TextView
     8         android:id="@+id/textView1"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:layout_alignParentLeft="true"
    12         android:layout_alignParentTop="true"
    13         android:layout_marginLeft="93dp"
    14         android:layout_marginTop="176dp"
    15         android:text="Large Text"
    16         android:textAppearance="?android:attr/textAppearanceLarge" />
    17 
    18 </RelativeLayout>

    很重要的AndroidManifest.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.example.test1"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk
     8         android:minSdkVersion="8"
     9         android:targetSdkVersion="17" />
    10 
    11     <application
    12         android:allowBackup="true"
    13         android:icon="@drawable/ic_launcher"
    14         android:label="@string/app_name"
    15         android:theme="@style/AppTheme" >
    16         <activity
    17             android:name="com.example.test1.MainActivity"
    18             android:label="@string/app_name" >
    19             <intent-filter>
    20                 <action android:name="android.intent.action.MAIN" />
    21 
    22                 <category android:name="android.intent.category.LAUNCHER" />
    23             </intent-filter>
    24         </activity>
    25         <activity android:name="com.example.test1.SecondActivity">
    26             <intent-filter>
    27                 <action android:name="aa.bb.cc.dd.ee.ff"/>
    28                 
    29                 <category android:name="android.intent.category.DEFAULT" />//隐式intent调用,这句必须加
    30             </intent-filter>
    31         </activity>
    32 
    33     </application>
    34 
    35 </manifest>
  • 相关阅读:
    java 抽象工厂模式简单实例
    java 工厂方法模式简单实例
    java 简单工厂模式实现
    tomcat管理页面上如何查看工程下的文件
    如何用Ecplise部署Web项目到tomcat中
    Servlet中操作文件
    ServletContext是什么
    model1模式变为mv模式,实现业务逻辑和画面的分离
    jdbc操作工具类
    Cookie技术随笔
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/3097153.html
Copyright © 2020-2023  润新知