Xamarin.Android 近接センサー
TYPE_PROXIMITY 近接センサ
![f:id:furuya02:20140529005407p:image:w150:left f:id:furuya02:20140529005407p:image:w150:left](http://cdn-ak.f.st-hatena.com/images/fotolife/f/furuya02/20140529/20140529005407.png)
近接センサは、ユーザがAndroidを耳に当てているかどうかをチェックするためのセンサーです。
手元の実機では、MaximumRangeが9となっていますが、取得できるデータは0か9の2値だけでした。耳を当てる部分にセンサーがあるらしく、そこから2〜3cmのところで反応しています。
機種によって、違いがあるようですが、どれも「0」か「それ以外」かの判断程度しかできないようです。
[Activity(Label = "AndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity, ISensorEventListener{ private SensorManager _senseManager;//センサーマネージャ private Sensor _proximitySensor;//近接センサー private TextView _textView; protected override void OnCreate(Bundle bundle){ base.OnCreate(bundle); SetContentView(Resource.Layout.Main); _textView = FindViewById<TextView>(Resource.Id.textView1); //センサーマネージャ取得 _senseManager = (SensorManager)GetSystemService(SensorService); //近接センサー取得 _proximitySensor = _senseManager.GetDefaultSensor(SensorType.Proximity); } protected override void OnResume(){ base.OnResume(); //リスナー登録 _senseManager.RegisterListener(this, _proximitySensor, SensorDelay.Ui); } protected override void OnPause(){ base.OnPause(); //リスナー解除 _senseManager.UnregisterListener(this); } //ISensorEventListenerで必須 public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy){} public void OnSensorChanged(SensorEvent e){ //近接センサーに関するものだけが対象 if (e.Sensor.Type == SensorType.Proximity){ var sb = new System.Text.StringBuilder(); //センサー名 sb.Append(string.Format("Name:{0}\r\n", _proximitySensor.Name)); //レンジ sb.Append(string.Format("MaximumRange:{0}\r\n", _proximitySensor.MaximumRange)); //現在のデータ sb.Append(string.Format("Value:{0}\r\n", e.Values[0])); _textView.Text = sb.ToString(); } } }
Xamarin.Android インストールされているアプリケーションの列挙
1 アプリケーション列挙
![f:id:furuya02:20140528060106p:image:w150:left f:id:furuya02:20140528060106p:image:w150:left](http://cdn-ak.f.st-hatena.com/images/fotolife/f/furuya02/20140528/20140528060106.png)
パッケージマネージャで条件となるインテントを指定してQueryIntentActivities()を読みだすと、条件にヒットしたアプリケーションの一覧が列挙されます。
現在インストールされているアプリケーションを列挙するためには、その条件として下記の2つを指定します。
ACTION_MAIN(アプリケーションの起動のエントリーポイント)
CATEGORY_LAUNCHER(トップレベルのランチャーに表示されるべきもの)
そう、これは通常アプリのマニュフェストで指定しているintent-filterそのものです。
<activity android:icon="@drawable/icon" android:label="AndroidApplication1" android:name="androidapplication1.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
この条件を指定して列挙をかけた例が下記のとおりです。
//起動用アクティビティの定義 // Intent.ActionMain:起動可能なインテント var mainIntent = new Intent(Intent.ActionMain,null); // Intent.CategoryLauncher デスクトップから可能なインテント(通常アプリ) mainIntent.AddCategory(Intent.CategoryLauncher); //アプリ一覧の取得 var ar = PackageManager.QueryIntentActivities(mainIntent, 0); var list = new List<ListItem>(); foreach (var a in ar) { //アイコン var icon = a.ActivityInfo.LoadIcon(PackageManager); //アプリ名 var name = a.LoadLabel(PackageManager); //アクティビティ名 var fullName = a.ActivityInfo.Name; list.Add(new ListItem() { Icon = icon, Name = name, FullName = fullName }); } //リストビューへのアダプタ設定 var listView = FindViewById<ListView>(Resource.Id.listView1); listView.Adapter = new MyAdapter(this, 0, list);
2 アプリケーションの管理(詳細)画面
![f:id:furuya02:20140528060107p:image:w150:left f:id:furuya02:20140528060107p:image:w150:left](http://cdn-ak.f.st-hatena.com/images/fotolife/f/furuya02/20140528/20140528060107.png)
次に、リストビューをクリックした際に、アプリケーションの管理(詳細)画面に遷移する例です。
SetDataでアプリケーションのパッケージ名、SetComponentでアプリケーション管理の詳細画面を指定したインテントでアクティビティを起動しています。
詳細画面では、アプリのアンインストールや、デフォルト設定などを行うことができます。
//リストビュー(アイテム)をクリックした際のイベントを定義 listView.ItemClick += (s, a) =>{ var intent = new Intent(); //アプリケーションのパッケージ名 var fullName = ar[a.Position].ActivityInfo.PackageName; intent.SetData(Android.Net.Uri.FromParts("package", fullName, null)); //アプリケーション管理の詳細画面 var cn = ComponentName.UnflattenFromString("com.android.settings/.applications.InstalledAppDetails"); intent.SetComponent(cn); //明示的アクティビティの起動 StartActivity(intent); };
3 AdapterList
参考に、今回リストビューで使用したアダプタのコードです。
Xamarin.Android ListViewの2.表示内容のカスタマイズを少し修正しただけのものです
public class ListItem { public Drawable Icon; //アイコン public string Name; //名前 public string FullName; //アクティビティ名 } public class MyAdapter : ArrayAdapter<ListItem> { private readonly LayoutInflater _layoutInflater; public MyAdapter(Context context, int rid, IList<ListItem> list) : base(context, rid, list) { _layoutInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService); } public override View GetView(int position, View convertView, ViewGroup parent) { //データを取り出す var item = GetItem(position); //レイアウトファイルからViewを作成 var view = _layoutInflater.Inflate(Resource.Layout.list_item, null); //アイコン var icon = view.FindViewById<ImageView>(Resource.Id.imageView); icon.SetImageDrawable(item.Icon); //名前 view.FindViewById<TextView>(Resource.Id.textViewName).Text = item.Name; //アクティビティ名 view.FindViewById<TextView>(Resource.Id.textViewFullName).Text = item.FullName; return view; } }