第一行代码系列第二章——使用隐式Intent

1效果图(与显式相同)


代码:

修改菜单文件中的SecondActivity

 <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.activitytest.Action_START"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
满足activity与category事件响应
将FirstActivity中的响应事件代码修改

Button button1 = (Button) findViewById(R.id.button_1);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast .makeText(FirstActivity .this, "you click the button 1", Toast.LENGTH_SHORT).show();
			    Intent intent = new Intent("com.example.activitytest.Action_START");
			    startActivity(intent);
			}
		});


每个Intent只能有一个action,但可以有多个category

下面在增加一个category

在FirstActivity中修改按钮响应

@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast .makeText(FirstActivity .this, "you click the button 1", Toast.LENGTH_SHORT).show();
			    Intent intent = new Intent("com.example.activitytest.Action_START");
			    intent.addCategory("com.example.activitytest.MY_CATEGORY");
			    startActivity(intent);
			}

在菜单文件中加入对应category\

<activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.activitytest.Action_START"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="com.example.activitytest.MY_CATEGORY"/>
            </intent-filter>
        </activity>



猜你喜欢

转载自blog.csdn.net/asdaosidasu/article/details/52502635