Xamarin.Androidでインテントを受け取る
2018年2月7日 (水) 05:11時点におけるimported>Administratorによる版 (ページの作成:「 Xamarin.Androidでインテントを受け取るにはActivityにIntentFilter属性を付ける。 JavaのようにAndroidManifest.xmlを編集してintent-filterを書...」)
Xamarin.Androidでインテントを受け取るにはActivityにIntentFilter属性を付ける。 JavaのようにAndroidManifest.xmlを編集してintent-filterを書くのはダメ。AndroidManifest.xmlを直接編集するとリフレクションに失敗して以下のような例外が発生する。これに気が付かずAndroidManifest.xmlの記述を間違えたかと思ってクソはまった。
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "アクティビティ名"
OnNewIntentやOnResumeなどのその他の部分はJavaと同じ。迷わずググれ。
[Activity(
Label = "MainActivity",
MainLauncher = true,
Icon = "@drawable/icon",
Theme = "@style/Theme.Splash",
AlwaysRetainTaskState = true,
LaunchMode = LaunchMode.SingleInstance,
ScreenOrientation = ScreenOrientation.Portrait,
ConfigurationChanges = ConfigChanges.Orientation |
ConfigChanges.KeyboardHidden |
ConfigChanges.Keyboard |
ConfigChanges.ScreenSize |
ConfigChanges.ScreenLayout)]
[IntentFilter(
new [] { Intent.ActionView },
Categories = new [] {
Intent.CategoryDefault,
Intent.CategoryBrowsable,
},
DataScheme = "myapp",
DataHost = "callback",
DataPath = ""
)]
public class MainActivity : Activity
{
protected override void OnResume()
{
base.OnResume();
var intent = this.Intent;
var action = intent.Action;
if (action == Intent.ActionView)
{
var uri = intent.Data;
if (uri != null)
{
}
}
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
this.Intent = intent;
}
// 以下略
}