20170127 Intent運用(啟動器)
//20170127 劉彥廷
//Intent運用(啟動器)
package com.example.user.myapplication46;
MainActivity.java
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v){
Intent it = new Intent(Intent.ACTION_VIEW);
switch (v.getId()){
case R.id.buttonEmail:
it.setData(Uri.parse("mailto:msze51899@gamil.com"));
it.putExtra(Intent.EXTRA_SUBJECT,"資料已收到"); //設定主旨
it.putExtra(Intent.EXTRA_TEXT,"您好,\n已收到,謝謝!!"); //設定內容
break;
case R.id.buttonSms:
it.setData(Uri.parse("sms:0988-025329?body=你好!"));
break;
case R.id.buttonWeb:
it.setData(Uri.parse("http:www.yahoo.com.tw"));
break;
case R.id.buttonGps:
it.setData(Uri.parse("geo:25.047095,121.517308"));
break;
case R.id.buttonWebSearch:
it.setAction(Intent.ACTION_WEB_SEARCH); //將動作改為收尋
it.putExtra(SearchManager.QUERY,"奇摩股市");
break;
}
startActivity(it);
}
}
//Intent運用(啟動器)
package com.example.user.myapplication46;
MainActivity.java
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v){
Intent it = new Intent(Intent.ACTION_VIEW);
switch (v.getId()){
case R.id.buttonEmail:
it.setData(Uri.parse("mailto:msze51899@gamil.com"));
it.putExtra(Intent.EXTRA_SUBJECT,"資料已收到"); //設定主旨
it.putExtra(Intent.EXTRA_TEXT,"您好,\n已收到,謝謝!!"); //設定內容
break;
case R.id.buttonSms:
it.setData(Uri.parse("sms:0988-025329?body=你好!"));
break;
case R.id.buttonWeb:
it.setData(Uri.parse("http:www.yahoo.com.tw"));
break;
case R.id.buttonGps:
it.setData(Uri.parse("geo:25.047095,121.517308"));
break;
case R.id.buttonWebSearch:
it.setAction(Intent.ACTION_WEB_SEARCH); //將動作改為收尋
it.putExtra(SearchManager.QUERY,"奇摩股市");
break;
}
startActivity(it);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.myapplication46.MainActivity"
android:orientation="vertical">
<Button
android:text="@string/Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonEmail"
android:onClick="onClick" />
<Button
android:text="@string/Button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonSms"
android:onClick="onClick" />
<Button
android:text="@string/Button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonWeb"
android:onClick="onClick" />
<Button
android:text="@string/Button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonGps"
android:onClick="onClick" />
<Button
android:text="@string/Button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonWebSearch"
android:onClick="onClick" />
</LinearLayout>
strings.xml
<resources>
<string name="app_name">APP 啟動器(No46)</string>
<string name="Button">啟動電子郵件</string>
<string name="Button2">啟動簡訊</string>
<string name="Button3">啟動網頁</string>
<string name="Button4">啟動地圖</string>
<string name="Button5">收尋Web資料</string>
</resources>
留言
張貼留言