Android Wearの通知から呼び出し元とは異なるIntentを起動します。
【準備】
1.「Extrasライブラリ(Android Support Library v20等)のアップデート」の手順で、「Android Support Library」をv20に更新しておきます。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndroidWearSample02-002-ContentIntentOtherIntent」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | com.example.androidwearsample02_002_contentintentotherintent |
| プロジェクト名(Project Name) | AndroidWearSample02-002-ContentIntentOtherIntent |
| パッケージ名(Package Name) | com.example.androidwearsample02_002_contentintentotherintent |
| Build SDK | API 8 |
| Minimum Required SDK | API 8 |
2.「起動アイコンの作成方法」の手順で、起動アイコンを作成。
3.「通知アイコンの作成方法」の手順で、通知アイコンを作成。
【手順2】
1.「AndroidManifest.xml」は以下の通り。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidwearsample02_002_contentintentotherintent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme" >
<activity android:name="com.example.androidwearsample02_002_contentintentotherintent.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.androidwearsample02_002_contentintentotherintent.SecondActivity" >
</activity>
</application>
</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順3】
1.「res/layout/activity_main.xml」は以下の通り。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickButton"
android:text="通知" />
</RelativeLayout>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順4】
1.「任意のファイルの作成方法」の手順で「res/layout/activity_second.xml」を作成。
2.「res/layout/activity_second.xml」は以下の通り。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2つめのアクティビティー" />
</RelativeLayout>
3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。4.「Ctrl+S」を押し、ファイルを保存。
【手順5】
1.「src/com/example/androidwearsample02_002_contentintentotherintent/MainActivity.java」は以下の通り。
package com.example.androidwearsample02_002_contentintentotherintent;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationManagerCompat;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickButton(View view) {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Builder builder = new Builder(this).setSmallIcon(R.drawable.ic_notify)
.setContentTitle("Intent起動サンプル")
.setContentText("Intentを起動します。")
.setContentIntent(pendingIntent);
int notificationId = 1;
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順6】
1.「任意のファイルの作成方法」の手順で「src/com/example/androidwearsample02_002_contentintentotherintent/SecondActivity.java」を作成。
2.「src/com/example/androidwearsample02_002_contentintentotherintent/SecondActivity.java」は以下の通り。
package com.example.androidwearsample02_002_contentintentotherintent;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。4.「Ctrl+S」を押し、ファイルを保存。
【手順7】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
【手順8】
1.以下の様に表示されるので、通知ボタンを押します。
以上です。




0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。