隐式调用Activity
最后更新于:2022-04-01 14:43:16
其实这部分知识点不难,只不过感觉有点多而且杂。这里就写个简单的action和category匹配吧。
代码很快就写完了,写了两个Activity,不过测试总是ANF错误,坑爹!之后查了很多资料才知道如果自己定义的某个Activity要通过隐式调用,在AndroidManifast.xm那么必须加上android.intent.category.DEFAULT,否则不起作用,因为系统会默认给你加上android.intent.category.DEFAULT这货。intent filter这货允许你添加多个category条目,如果有一个category不匹配系统就会判定不匹配。
写的有点乱,下面贴代码:
两个Activity:
~~~
package com.example.mytest;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) this.findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("nextActivity");
intent.addCategory("haha");
startActivity(intent);
}
});
}
}
~~~
~~~
package com.example.mytest;
import android.app.Activity;
import android.os.Bundle;
public class NextActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next_activity);
}
}
~~~
下面是清单文件:
~~~
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mytest.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="com.example.mytest.NextActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="nextActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="haha" />
</intent-filter>
</activity>
</application>
</manifest>
~~~