例えばブラウザアプリの共有メニューとかTwitterの共有メニューから自分のアプリを起動するにはどうすればいいのだろうか?
答えは、Intent機能を使えばいけそうだ。
<!-- AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="..."
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="..."
android:label="..."
android:parentActivityName="..." >
<meta-data
.. />
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application>
</manifest>AndroidManifest.xmlで共有時に開くactivityの設定と共有項目の設定を行う。@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
//intent.getStringExtra(Intent.EXTRA_TEXT)
}
}else
}
}上は共有元から呼ばれた時の処理
0 コメント:
コメントを投稿