- 在MainActivity代碼中使用kotlin語言進行跳轉:
Github Demo 下載
fun jump(view: View) {
val intent = Intent()
//獲取intent對象
intent.setClass(this,Main2Activity::class.java)
// 獲取class是使用::反射(那么問題來了,反射是個什么鬼?????????小白的悲哀啊,趕緊研究研究去)
startActivity(intent)
}
- 在MainActivity代碼中使用Java語言進行跳轉:
原文鏈接
M1. 通過class跳轉
Intent intent = new Intent();
//(當前Activity,目標Activity)
intent.setClass(MainActivity.this, TestActivity.class);
startActivity(intent);
M2、為Activity設置Action,通過Action跳轉
Intent intent = new Intent();
//(聲明中的Action)
intent.setAction("gesoft_cx");
//(聲明中的Category)
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
上面的Action和Category應該與Activity聲明中設置的Action和Category相同
<activity android:name="com.cx.testdemo.TestActivity">
<!-- intent-filter:過濾器一 -->
<intent-filter >
<!-- action隨便設置,但必須保證唯一 -->
<action android:name="gesoft_cx"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
M3、跳轉到其他應用界面
Intent intent = new Intent();
(目標應用的包名,目標應用的目標Activity)
intent.setClassName("com.imooc.mooo", "com.imooc.mooo.MainListActivity");
startActivity(intent);
目標Activity需要添加exported="true"屬性
M4、自定義權限
如果目標應用不希望其他應用跳轉到 自身頁面,可以增加自定義權限:1)在Activity聲明標簽中增加permission(權限)屬性,屬性值隨便設置,但必須唯一
<activity
android:permission="gesoft.cx"
android:name="com.imooc.mooo.MainListActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
增加自定義權限,屬性name的值與上面permission屬性值相同,屬性protectionLevel(安全級別)根據需要(普通normal)設置
<!-- 自定義權限,防止其他應用訪問 -->
<permission
android:name="gesoft.cx"
android:protectionLevel="normal">
</permission>
這時在使用3中的方法進行跳轉就會報錯。如果還要訪問就必須在當前應用中添加一個與目標應用相對應的權限:
<uses-permission android:name="gesoft.cx" />
注意:將權限name屬性值設為"gesoft_cx"時,出現錯誤。
M5、頁面跳轉與數據回傳
A啟動B,B頁面中的數據可以回傳給AstartActivityForResult(intent, requestCode);//requestCode
請求的標識//resultCode:第二個頁面返回的標識//data:第二個頁面回傳的數據A頁面通過
onActivityResult(int requestCode, int resultCode, Intent data)
方法接收回傳過來的數據B頁面通過setResult(resultCode, data)
方法回傳數據給A頁面
代碼示例:
1、布局文件,兩個Activity通用該布局文件。
<RelativeLayout 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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="跳轉頁面" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1" />
</RelativeLayout>
2、第一個Activity代碼
package com.cx.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
textView = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MyActivity.class);
/**
* 第一個參數是Intent對象
* 第二個參數是請求的一個標識
*/
startActivityForResult(intent, 1);
}
/**
* 通過startActivityForResult跳轉,接收放回數據的方法
* requestCode:請求的標識
* resultCode:第二個頁面返回的標識
* data:第二個頁面回傳的數據
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == 1 && resultCode == 2){
String content = data.getStringExtra("data");
textView.setText(content);
}
}
}
3、第二個Activity頁面代碼
package com.cx.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyActivity extends Activity implements OnClickListener{
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setText("回傳數據");
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//回傳到第一個頁面的數據實際上是一個Intent對象
Intent data = new Intent();
data.putExtra("data", "回傳數據");
setResult(2, data);
finish();
}
}
不要忘記Activity的聲明注冊。。。