2016年11月25日

Android 禁止螢幕旋轉

Android 禁止螢幕旋轉


AndroidManifest.xml設置
android:screenOrientation="portrait"  (強制直向)
android:screenOrientation="landscape"  (強制橫向)


Java Code設置
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//畫面直向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//畫面橫向


以上畫面只要轉方向時,activity 會重新執行。



1、如果不要 activity 重新執行,需要在 AndroidManifest.xml 檔案中,在 activity 加入 android:configChanges="orientation|keyboardHidden" 就會禁止重新執行。

這個屬性指的是,當後邊屬性值代表的事件發生時,Activity 會執行某個函數,orientation 指的是當螢幕旋轉時,keyboardHidden 指的是鍵盤輔助功能改變。“|”為或符號,指這兩個中任意一個發生,就執行 Activity 某個函數。
如果你的開發 API 等級等於或高於 13,你還需要設置 screenSize,因為 screenSize 會在螢幕旋轉時改變。


2、在對應 Activity 中重寫 onConfigurationChanged() 方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // 什麼都不用寫
    }
    else {
        // 什麼都不用寫
    }
}

如果在 if、else 中,使用了 setContentView(R.layout.xxxx) 函數,那麼就可以實現:每次螢幕旋轉時,調用不同的佈局。


3、動態更改螢幕方向:

在程式中有需要的時候旋轉螢幕,例如
@Override
public void onClick(View v) {
    // 如果是豎排,則改為橫向
    if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    // 如果是橫排,則改為直向
    else if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}
重點就是:getRequestedOrientation() 函數、setRequestedOrientation() 函數的使用。
注意:使用這種方法,必須事先在 AndroidManifest.xml 的 <activity> </activity> 中,添加android:screenOrientation 屬性值,不然 getRequestedOrientation() 可能會出問題。

2016年11月23日

Android Intent用法

Android Intent用法

顯示網頁
Uri uri = Uri.parse("http://google.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);


使用郵件MAIL
//傳送文字
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, "emailaddress@emailaddress.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");
startActivity(Intent.createChooser(intent, "Send Email"));

//傳送影音附件檔
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/mysong.mp3"));
it.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));

//傳送圖片附件檔
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/mypic.jpg"));
it.setType("image/jpeg");
startActivity(Intent.createChooser(it, "Choose Email Client"));


撥打電話
//叫出撥號程式
Uri uri = Uri.parse("tel:0800000123");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
//直接打電話出去
Uri uri = Uri.parse("tel:0800000123");
Intent it = new Intent(Intent.ACTION_CALL, uri);
startActivity(it);
//用這個,要在 AndroidManifest.xml 中,加上
//<uses-permission id="android.permission.CALL_PHONE" />



顯示聯絡人清單
Intent it = new Intent(Intent.ACTION_VIEW, People.CONTENT_URI);
startActivity(it);



播放多媒體
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);


安裝 APK 檔
Uri uri = Uri.parse("url_of_apk_file");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.setData(uri);
it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
it.setClassName("com.android.packageinstaller",
                "com.android.packageinstaller.PackageInstallerActivity");
startActivity(it);
//make sure the url_of_apk_file is readable for all users

2016年11月19日

Android 常用系統預設文字大小以及顏色

Android 常用系統預設文字大小以及顏色


<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


android:textAppearance="?android:attr/textAppearanceLarge"  或是
sytle="?android:attr/textAppearanceLarge" 實際上是一樣的。


文字大小
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAppearance="?android:attr/textAppearanceSmall"

字體顏色
android:textColor="?android:attr/textColorPrimary"
android:textColor="?android:attr/textColorSecondary"
android:textColor="?android:attr/textColorTertiary"
android:textColor="?android:attr/textColorPrimaryInverse"
android:textColor="?android:attr/textColorSecondaryInverse"

2016年11月14日

Android studio測試問題 測試會出現安裝兩個相同APP

Android studio測試問題 測試會出現安裝兩個相同APP

問題出現在AndroidManifest.xml寫入2個Activity<intent-filter>
只要刪除底下紅色字體代碼即可以修正錯誤,正常測試。
AndroidManifest.xml


<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity
    android:name=".SetupActivityListSample"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

2016年11月7日

Android AlertDialog

Android AlertDialog

建立AlertDialog對話盒

AlertDialog.Builder builder  = new AlertDialog.Builder(gameViewA.getContext());
builder.setTitle("抬頭訊息...");
builder.setMessage("本文訊息");

builder.setNegativeButton("右邊按鈕", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
  }
});
builder.setNeutralButton("中間按鈕", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
  }
});
 builder.setPositiveButton("左邊按鈕", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
  }
});
 AlertDialog alert = builder.create();
 alert.show();

2016年11月6日

Android SensorManager Accuracy Value

Android SensorManager Accuracy Value


public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }


int accuracy


int SENSOR_STATUS_ACCURACY_HIGH
This sensor is reporting data with maximum accuracy

Constant Value: 3 (0x00000003)


int SENSOR_STATUS_ACCURACY_LOW
This sensor is reporting data with low accuracy, calibration with the environment is needed

Constant Value: 1 (0x00000001)


int SENSOR_STATUS_ACCURACY_MEDIUM
This sensor is reporting data with an average level of accuracy, calibration with the environment may improve the readings

Constant Value: 2 (0x00000002)

int SENSOR_STATUS_NO_CONTACT
The values returned by this sensor cannot be trusted because the sensor had no contact with what it was measuring (for example, the heart rate monitor is not in contact with the user).

Constant Value: -1 (0xffffffff)


int SENSOR_STATUS_UNRELIABLE
The values returned by this sensor cannot be trusted, calibration is needed or the environment doesn't allow readings

Constant Value: 0 (0x00000000)