android显式启动与隐式启动

package com.example.start_5;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button btn1;

	private Button btn2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btn1 = (Button) findViewById(R.id.button1);

		btn2 = (Button) findViewById(R.id.button2);

		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根/
				// /显示启动

				Intent intent = new Intent(MainActivity.this, Layout2.class);
				startActivity(intent);
				Toast.makeText(MainActivity.this, "显示启动", Toast.LENGTH_SHORT)
						.show();

			}
		});
		btn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根/

				// //隐士启动
				Intent intent = new Intent("com.example.start_5.Layout2");

				startActivity(intent);
				Toast.makeText(MainActivity.this, "隐士启动", Toast.LENGTH_SHORT)
						.show();
			}
		});
	}
}
package com.example.start_5;

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

public class Layout2 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout2);
	}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.start_5"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Layout2"
            android:label="第二个界面" >
            <intent-filter>
                <action android:name="com.example.start_5.Layout2" />    //这里要是完整路径

                <category android:name="android.intent.category.DEFAULT" />  //这个要是DEFAULT

            </intent-filter>
        </activity>
    </application>

</manifest>



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android 中,启动 Service 时,可以使用显式 Intent隐式 Intent隐式 Intent显式 Intent 的区别在于,隐式 Intent 不指定要启动的 Service 的类名,而是指定一组 Intent 过滤器,让系统自动找到满足条件的 Service。如果有多个 Service 满足条件,则会抛出异常。 要使用隐式 Intent 启动 Service,可以使用以下代码: ``` Intent intent = new Intent("com.example.action.MY_SERVICE"); intent.setPackage("com.example.myapp"); startService(intent); ``` 在这个例子中,我们创建了一个隐式 Intent,通过调用 Intent 的 setPackage() 方法指定了要启动的 Service 所在的应用程序包名。这样系统就会在指定的包中寻找满足 Intent 过滤器条件的 Service,如果找到了一个或多个 Service,则会启动第一个匹配的 Service。 需要注意的是,为了使用隐式 Intent 启动 Service,必须在 Service 的 AndroidManifest.xml 文件中定义一个或多个 Intent 过滤器,以便系统能够匹配到该 Service。例如: ``` <service android:name=".MyService"> <intent-filter> <action android:name="com.example.action.MY_SERVICE" /> </intent-filter> </service> ``` 在这个例子中,我们为 MyService 定义了一个 Intent 过滤器,其中包含一个 action,对应上面隐式 Intent 中指定的 "com.example.action.MY_SERVICE"。这样,当系统收到一个 action 为 "com.example.action.MY_SERVICE" 的 Intent 时,就会启动 MyService。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二十六画生的博客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值