Created
September 22, 2012 15:43
-
-
Save sakurabird/3766557 to your computer and use it in GitHub Desktop.
emailのリンクからアプリを起動するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" > | |
| <TextView | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_centerHorizontal="true" | |
| android:layout_centerVertical="true" | |
| android:text="@string/hello_world" | |
| tools:context=".MainActivity" /> | |
| </RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical" > | |
| <LinearLayout | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:orientation="horizontal" > | |
| <TextView | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="送信先アドレス:" /> | |
| <EditText | |
| android:id="@+id/address" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="xxx@gmail.com" /> | |
| </LinearLayout> | |
| <LinearLayout | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:orientation="horizontal" > | |
| <TextView | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="メールタイトル:" /> | |
| <EditText | |
| android:id="@+id/title" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="これはメールのタイトルです" /> | |
| </LinearLayout> | |
| <LinearLayout | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:orientation="horizontal" > | |
| <TextView | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="メールbody:" /> | |
| <EditText | |
| android:id="@+id/body" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="http://www.com.sakurafish.myexamplemaillink/something" /> | |
| </LinearLayout> | |
| <Button | |
| android:id="@+id/button1" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="メール送信" /> | |
| </LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| package="com.sakurafish.myexamplemaillink" | |
| android:versionCode="1" | |
| android:versionName="1.0" > | |
| <uses-sdk | |
| android:minSdkVersion="8" | |
| android:targetSdkVersion="15" /> | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| <application | |
| android:icon="@drawable/ic_launcher" | |
| android:label="@string/app_name" | |
| android:theme="@style/AppTheme" > | |
| <!-- メール送信のテスト用アクティビティ --> | |
| <activity | |
| android:name=".MainActivity" | |
| android:label="@string/title_activity_main" > | |
| <intent-filter> | |
| <action android:name="android.intent.action.MAIN" /> | |
| <category android:name="android.intent.category.LAUNCHER" /> | |
| </intent-filter> | |
| </activity> | |
| <activity | |
| android:name=".LaunchByMailActivity" | |
| android:label="@string/title_activity_main" > | |
| <intent-filter> | |
| <action android:name="android.intent.action.VIEW" /> | |
| <category android:name="android.intent.category.DEFAULT" /> | |
| <category android:name="android.intent.category.BROWSABLE" /> | |
| <!-- | |
| e-mail のリンクから起動するためのインテントフィルタの設定 | |
| g-mailなどのクライアントではhttp,httpsが付いていないとリンクとして認識してくれない。 | |
| そこで、Android側でschemeに"http"を付けて起動するようにしてしまう。 | |
| メール側では「http://www.com.sakurafish.myexamplemaillink/something」にリンクを貼る。 | |
| 注意:この設定をしてアプリを起動できるが、ユーザーがインテントでブラウザを選んでしまった場合 | |
| 404ページがみつかりません云々のエラーになってしまう。 | |
| 宛先ページを作成しておいてJavaScriptなどでリダイレクト処理を書いておくなどの処理が必要 | |
| --> | |
| <data | |
| android:host="www.com.sakurafish.myexamplemaillink" | |
| android:path="/something" | |
| android:scheme="http" /> | |
| </intent-filter> | |
| </activity> | |
| Ï | |
| </application> | |
| </manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.sakurafish.myexamplemaillink; | |
| import android.app.Activity; | |
| import android.os.Bundle; | |
| public class LaunchByMailActivity extends Activity { | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_launchbymail); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.sakurafish.myexamplemaillink; | |
| import android.app.Activity; | |
| import android.content.Intent; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.view.View.OnClickListener; | |
| import android.widget.Button; | |
| import android.widget.EditText; | |
| public class MainActivity extends Activity { | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| final EditText address = (EditText) findViewById(R.id.address); | |
| final EditText title = (EditText) findViewById(R.id.title); | |
| final EditText body = (EditText) findViewById(R.id.body); | |
| Button button = (Button) findViewById(R.id.button1); | |
| button.setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| Intent i = new Intent(Intent.ACTION_SEND); | |
| i.setType("message/rfc822"); | |
| i.putExtra(Intent.EXTRA_EMAIL, new String[] { | |
| address.getText().toString() | |
| }); | |
| i.putExtra(Intent.EXTRA_SUBJECT, title.getText().toString()); | |
| i.putExtra(Intent.EXTRA_TEXT, body.getText().toString()); | |
| startActivity(Intent.createChooser(i, "Send mail...")); | |
| } | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <resources> | |
| <string name="app_name">MyExampleMailLink</string> | |
| <string name="hello_world">起動成功!</string> | |
| <string name="menu_settings">Settings</string> | |
| <string name="title_activity_main">mailからアプリを起動するサンプル</string> | |
| </resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <resources> | |
| <style name="AppTheme" parent="android:Theme.Light" /> | |
| </resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment