2015年5月12日 星期二

android-利用 color.xml 控制所有套板顏色

想必有些開發者可能要面對很多奇怪的需求
例如 :
A公司背景要紅色
B公司背景要橙色
C公司背景要黃色
...等,面對如果只是5張頁面還可以
但是到達10張以上的頁面要怎辦?

當然客戶沒有很奇怪的需求的話
我們只要把版面集中控管就可以方便去處理了

1 . 首先我們在專案底下的Value新增 : color.xml

2 . 參數如下 :

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>

</resources>

3 . 在介面使用自己設定的參數




























4 .  如果是在 Java 底下設定
R.color 是專案底下的 Color . xml
view.setBackgroundColor(getResources().getColor(R.color.black));


以後只需要在 color.xml做更改
所有套用的頁面也會一起更改

參考資料 : How to set background color of a View

歡迎轉載,請註明出處。

2015年5月11日 星期一

Android-EditText 監聽 字數 限制

想必各位大大多少會遇到輸入字框的字數限制
這裡就為大家整理一份出來
( 未完成 )
1 . addTextChangedListener
監聽文字被改變時

public class MainActivity extends Activity {

    private String TAG = "EditTextSample";
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText1);
        editText.addTextChangedListener(new TextWatcher(){

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                Log.d(TAG,"之前是 : " + s);

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                Log.d(TAG,"文字被改變時 : " + s);

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.d(TAG,"之後為 : " + s);

            }});
    }
}















2 .

參考資料 : 來源1

2015年4月30日 星期四

Android-專案匯入 Library 一直失敗

如果是開發者多少都會使用到第三方的Lib
將Lib匯入 但是欄位內一直被畫叉叉
導致一直無法使用開發包

有可能的情況是 :
Eclipse 的 Workspace 下,沒有此Library
是的,所以您可能下載完的位置無法被Eclipse辨別
如果不懂,那就直接把Library丟到WorkSpace下

參考資料 : 來源

歡迎轉載,請註明出處

Android-撥放串流影音( Play RTSP Vedio )範例連結

各位大大使否在測試影音串流找不到測試來源
以下皆是測試的連結
目前整理出來的都可以使用
連結一 : 位置
連結二 : 位置

2015年4月29日 星期三

Android-使用 bindService 範例 Example

更新日期 : 20150827 新增範例程式碼

1 . onServiceDisconnected 何時有CallBack :
並非 unBindService 就會觸發,此回應是Service 被異常銷毀(資源不足...等)

2 . IPC : 進程之間的通信 ( activity 和 Service 之間的參數傳遞 )

3 . activity onDestory 後 bindService 所執行的 Service 也會跟者終止

-------------------------------------
導讀 :
1 . 建立帶有 Bind 的 Service
2 . 在 Activity 內建立 ServiceConnection 物件
     在  Connection 的 CallBack 將 Service 建立出來
3 . 啟動 Service 和 帶入剛剛生成的物件
4 . 可控制 Service 了

This is a easy Sample

範例 :
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bind" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ServiceMethod" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unBind" />

</LinearLayout>


MainActivity.java
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener{

    private String TAG = "MainService";
    
    private Button btn_1;
    private Button btn_2;
    private Button btn_3;
    
    public MainService myService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_1 = (Button)findViewById(R.id.button1);
        btn_2 = (Button)findViewById(R.id.button2);
        btn_3 = (Button)findViewById(R.id.button3);
        
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        
    }

    @Override
    public void onClick(View view) {

        switch(view.getId())
        {
        case R.id.button1:

            // 綁定 Service
            Intent serviceIntent = new Intent(this, MainService.class);
            this.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);

            break;

        case R.id.button2:
            // 使用 Service 內的方法
            myService.uu();
            
            break;
            
        case R.id.button3:
            
            // 解除綁定 Service
            try{
                this.unbindService(connection);    
            } catch(Exception e){
                Log.d(TAG, e.toString());
            }
            
            break;
        }

    }

    public ServiceConnection connection = new ServiceConnection() {

        // 成功與 Service 建立連線
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myService = ((MainService.MyBinder) service).getService();
            Log.d(TAG, "MainActivity onServiceConnected");
        }

        // 與 Service 建立連線失敗
        @Override
        public void onServiceDisconnected(ComponentName name) {
            myService = null;
            Log.d(TAG, "MainActivity onServiceFailed");
        }
    };
}

MainService.java
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MainService extends Service { 

    private String TAG = "MainService";

    public MyBinder myBinder = new MyBinder(); 

    // 綁定此 Service 的物件
    public class MyBinder extends Binder { 
        public MainService getService() { 
            return MainService.this; 
        } 
    } 

    // 綁定
    @Override 
    public IBinder onBind(Intent intent) { 
        Log.d(TAG, "MainService onBind");

        return myBinder; 
    } 

    // 解除綁定
    @Override 
    public boolean onUnbind(Intent intent) { 
        Log.d(TAG, "MainService onUnbind");
        return super.onUnbind(intent); 
    } 

    @Override 
    public void onCreate() { 
        super.onCreate(); 
        Log.d(TAG, "MainService onCreate");
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
        Log.d(TAG, "MainService onStartCommand");
        return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public void onDestroy() { 
        super.onDestroy(); 
        Log.d(TAG, "MainService onDestroy");
    } 

    // Service 測試用的 Method
    public void uu(){
        Log.d(TAG, "MainService uu");
    }
} 

Finish :






參考資料 : 來源
參考資料 : 程式碼

歡迎轉載,請註明出處。

2015年4月28日 星期二

Android-監聽開機後啟動應用程式

開機監聽

情境說明 :
        小名設計一個背景服務的App,但如果使用者關機那不是就無法啟動了,
這Sample就針對於小名所遇到的狀況做的。

------------ 廣播接收服務 -------------------

public class MyBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            Intent bootActivityIntent=new Intent(context,MainActivity.class);
            bootActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(MainActivity);
        }
    }
}

------------ 註冊廣播接收服務 -------------------

<receiver android:name=".MyBroadCastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>



參考資料 : 來源