C50108
Search…
2019/0611/安卓工作室_(意圖+隱含意圖)資料傳遞+Android生命週期+硬體裝置與應用

StartActivity 只能傳過去,要回傳必須用另外一種方法。
StartActivityForResult()取得活動的回傳資料。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intent03">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".OpActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

package com.example.intent03;
import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final int SET_RESULT = 1;
private TextView output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = findViewById(R.id.lblOutput);
}
public void button_Click(View v){
EditText txtOpd1,txtOpd2;
txtOpd1 = findViewById(R.id.txtOpd1);
txtOpd2 = findViewById(R.id.txtOpd2);
Intent intent = new Intent(this,OpActivity.class);
Bundle bundle = new Bundle();
bundle.putString("OPERAND01",txtOpd1.getText().toString());
bundle.putString("OPERAND02",txtOpd2.getText().toString());
intent.putExtras(bundle);
startActivityForResult(intent,SET_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case SET_RESULT:
if(resultCode == RESULT_OK){
Bundle bundle = data.getExtras();
output.setText("計算結果: "+ bundle.getDouble("RESULT"));
}
break;
}
}
}

package com.example.intent03;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
public class OpActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_op);
}
public void button2_Click(View v) {
int opd1, opd2;
double result = 0.0;
RadioButton rdbAdd,rdbSubtarct , rdbMultiply,rdbDivide;
CheckBox chkDivide;
Bundle bundle = this.getIntent().getExtras();
if (bundle == null)
return;
opd1= Integer.parseInt(bundle.getString("OPERAND01"));
opd2= Integer.parseInt(bundle.getString("OPERAND02"));
rdbAdd = findViewById(R.id.rdbAdd);
if (rdbAdd.isChecked()){
result = opd1 + opd2;
}
rdbSubtarct = findViewById(R.id.rdbSubtract);
if (rdbSubtarct.isChecked()){
result = opd1 - opd2;
}
rdbMultiply = findViewById(R.id.rdbMultiply);
if (rdbMultiply.isChecked()){
result = opd1 * opd2;
}
rdbDivide = findViewById(R.id.rdbDivide);
chkDivide = findViewById(R.id.chkDivide);
if (rdbDivide.isChecked()){
if (chkDivide.isChecked())
result = opd1 / opd2;
else
result = opd1 / (double)opd2;
}
Intent rIntent = new Intent();
Bundle rbundle = new Bundle();
rbundle.putDouble("RESULT",result);
rIntent.putExtras(rbundle);
setResult(RESULT_OK,rIntent);
finish();
}
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
>
<TextView
android:text="運算元(一):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/txtOpd1"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:layout_alignBaseline="@+id/textView"
android:layout_alignBottom="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:text="100" />
<TextView
android:text="運算元(二):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtOpd1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="33dp"
android:id="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_below="@+id/txtOpd1"
android:layout_alignLeft="@+id/txtOpd1"
android:layout_alignStart="@+id/txtOpd1"
android:layout_marginTop="22dp"
android:id="@+id/txtOpd2"
android:text="50" />
<Button
android:text="選擇運算子"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtOpd2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="31dp"
android:id="@+id/button"
android:onClick="button_Click" />
<TextView
android:text="運算結果"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/lblOutput" />
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_op"
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"
>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="horizontal"
android:id="@+id/rg">
<RadioButton
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rdbAdd"
android:layout_weight="1" />
<RadioButton
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rdbSubtract"
android:layout_weight="1" />
<RadioButton
android:text="*"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rdbMultiply"
android:layout_weight="1" />
<RadioButton
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rdbDivide"
android:layout_weight="1" />
</RadioGroup>
<CheckBox
android:text="整數除法"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="47dp"
android:id="@+id/chkDivide"
android:layout_below="@+id/rg"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="計算"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:id="@+id/button2"
android:onClick="button2_Click"
android:layout_below="@+id/chkDivide"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>

Application Installation Failed in Android Studio
Stack Overflow

package com.example.intent04;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button_Click(View v){
EditText url = findViewById(R.id.txtURL);
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse(url.getText().toString()));
startActivity(i);
}
public void button2_Click(View v){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("tel:+073427156"));
startActivity(i);
}
public void button3_Click(View v){
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:25.04775,121.51705"));
startActivity(i);
}}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
>
<TextView
android:text="URL:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="http://www.google.com"
android:ems="10"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="17dp"
android:id="@+id/txtURL" />
<Button
android:text="啟動瀏覽器"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtURL"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="28dp"
android:id="@+id/button"
android:onClick="button_Click" />
<Button
android:text="打電話"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:id="@+id/button2"
android:onClick="button2_Click"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="啟動地圖"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:id="@+id/button3"
android:onClick="button3_Click"
android:layout_below="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

package com.example.intent05;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int GET_CONTACT =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button_Click(View view) {
EditText key = findViewById(R.id.txtKeyword);
Intent i = new Intent(Intent.ACTION_WEB_SEARCH);
i.putExtra(SearchManager.QUERY,key.getText().toString());
startActivity(i);
}
public void button1_Click(View view) {
Intent i = new Intent(Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(i,GET_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GET_CONTACT) {
if(resultCode == RESULT_OK){
String uri = data.getData().toString();
Toast.makeText(this,uri,Toast.LENGTH_LONG).show();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
}
}
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
>
<TextView
android:text="關鍵字:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Android"
android:ems="10"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="18dp"
android:id="@+id/txtKeyword" />
<Button
android:text="Web搜尋"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtKeyword"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:id="@+id/button"
android:onClick="button_Click" />
<Button
android:text="選取聯絡人"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button1"
android:onClick="button1_Click" />
</RelativeLayout>
多了一個+如果有問題的畫

硬體裝置 陀螺儀
電力消耗 優先權運算 資源資訊 更新速度

package com.example.intent06;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Intent06","MainActivity:onCreate 方法被執行");
}
@Override
protected void onStart() {
super.onStart();
Log.d("Intent06","MainActivity:onStart 方法被執行");
}
@Override
protected void onResume() {
super.onResume();
Log.d("Intent06","MainActivity:onResume 方法被執行");
}
@Override
protected void onPause() {
super.onPause();
Log.d("Intent06","MainActivity:onPause 方法被執行");
}
@Override
protected void onStop() {
super.onStop();
Log.d("Intent06","MainActivity:onStop 方法被執行");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("Intent06","MainActivity:onRestart 方法被執行");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("Intent06","MainActivity:onDestroy 方法被執行");
}
}
Android Studio 筆記─設定模擬器AVD Manager - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天
iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天

非同步?
非同步 - Google Search
Google

接著 實作介面的方法

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.application2">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MusicActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

package com.example.application2;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MusicActivity extends AppCompatActivity implements MediaPlayer.OnCompletionListener{
private TextView output;
private MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
output = findViewById(R.id.lblOutput);
player = MediaPlayer.create(this,R.raw.piano01);
player.setOnCompletionListener(this);
}
@Override
public void onCompletion(MediaPlayer mp) {
output.setText("音樂播放完畢....");
player.seekTo(0);
}
public void button_Click(View v){
if (player != null){
if(player.isPlaying() != true){
player.start();
output.setText("音樂播放中...");
}
}
}
public void button2_Click(View v){
output.setText("音樂暫停播放...");
if (player != null){
player.pause();
}
}
public void button3_Click(View v){
output.setText("音樂停止播放...");
if (player != null){
player.stop();
player.prepareAsync();
}
}
@Override
public void onDestroy(){
super.onDestroy();
if (player != null){
player.release();
}
}
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
>
<Button
android:text="開始/繼續"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button"
android:onClick="button_Click" />
<Button
android:text="暫停"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:id="@+id/button2"
android:onClick="button2_Click" />
<Button
android:text="停止"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:id="@+id/button3"
android:onClick="button3_Click" />
<TextView
android:text="撥放狀態"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="33dp"
android:id="@+id/lblOutput" />
</RelativeLayout>

不同生命週期不同函式進行
例如畫面在背景則執行音樂暫停
切回前景則繼續撥放
桌面上777
SDK物件 OPEN GL ? 才能支援 不然會破圖
E:\android\Skd\emulator\emulator -avd 777 -gpu guest
Host
無法使用要改為 guest

package com.example.application2;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoActivity extends AppCompatActivity {
private VideoView video;
private String videoFile = "point21";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
video = findViewById(R.id.videoView);
// int id = getResources().getIdentifier(videoFile,"raw",this.getPackageName());
int id = R.raw.point21;
String path = "android.resource://" + this.getPackageName() + "/" + id;
video.setVideoURI(Uri.parse(path));
video.setMediaController(new MediaController(this));
video.start();
}
@Override
public void onPause(){
super.onPause();
video.stopPlayback();
}
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="媒體播放器"
android:id="@+id/textView" />
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp"
android:id="@+id/videoView" />
</RelativeLayout>

Copy link
On this page
#Java前情提要
#意圖 Intent
##SET_RESULT 是定義好的常數值
##RESULT _OK 是定義好的常數值
##事件處理函式 onAcitvityResult
##startActivityForResult生命週期
##隱含意圖資料回傳
##接著講 邊緣管理 狀態切換
##手機正轉倒畫面切換 會重新繪圖 執行一次生命週期
#Gps 相機 重力感應器等等…
外部裝置請求許可
##ViedoView、MediaController物件