一、显示Intent

  startActivity(new Intent(MainActivity.this,BAty.class));

  显示Intent直接指定要启动的Intent类

  注意自己通过创建一个java类,然后让其继承Activity时,只需在该类中添加onCreate重载函数,然后在其中设置setContentView(R.layout."自定义的xml布局文件") 

package com.example.shiyanshi.learnintent;

import android.app.Activity;
import android.os.Bundle;

/**
* Created by shiyanshi on 2015/12/28.
*/
public class MyAty extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
}
除此之外还要在AndroidManifest.xml中的application中添加<activity android:name=".MyAty"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro/>
</application>

</manifest>

二、隐式Intent

  通过在AndroidManifest.xml文件中指定<intent-filter>的action和category,然后在startActivity设置new Intent(“com.example.shiyanshi.learnintent.MyAty”).注意此处的字符串的名字要与action属性中name中的字符串一样。

通过这种隐式的Intent,可以在另外的一个app中调用这个app定义的activity,注意若不想让当前的activity被其他应用程序调用只需设置<activity android:name=".MyAty" android:exported="false">acitivity的exported属性为false(默认为真)。

 

可以通过try-catch语句捕获异常信息,并使用Toast.makeText(……).show();进行显示出来
try {
startActivity(new Intent("com.example.shiyanshi.learnintent.MyAty"));
}catch (Exception e){
Toast.makeText(MainActivity.this,"无法启动指定的Activity",Toast.LENGTH_SHORT).show();
}

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro>三、Intent过滤器相关选项
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro)));其中app://是指协议,后面紧跟的参数可以任意设置。
<activity
    android:name=".MyAty1"
android:label="@string/title_activity_my_aty1" >
<intent-filter>
<action android:name="com.example.shiyanshi.learnintent.MyAty"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="app"/>
</intent-filter>
</activity>
四、通过浏览器启动Activity
若想Activity被浏览器启动需设置红色标注的部分,在浏览器开发中href设置为“app://hello"(其中hello为任意设置的参数)。
<activity
    android:name=".LocalActivity"
android:label="@string/title_activity_local" >
<intent-filter>
<category android:name="ANDROID.INTENT.CATEGORY.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="app"/>
</intent-filter>
</activity>
若想获取浏览器的返回数据可以设置Uri uri=getIntent().getData();来返回一个Uri对象。
 






 

相关文章:

  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-28
  • 2021-04-11
  • 2022-02-05
  • 2022-01-14
  • 2021-10-14
  • 2022-12-23
  • 2021-07-17
相关资源
相似解决方案