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>



參考資料 : 來源

android-Service 執行線程工作無回應

若要在Service執行耗時的工作、被阻塞的工作
例如 : 延遲執行、卡住流程...等。
應用程式將會出現ANR(Application Not Responging)
簡單的說手機將一直靜止而導致無任何動作
為了避免以上的情況發生
建議可以製造一個新的獨立線程
讓主線程能夠順利地去執行

如果要在Service製造延遲效果可以如下 :

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        
        ThreadDemo td=new ThreadDemo();
        td.start();
        return START_STICKY;
    }
    
    private class ThreadDemo extends Thread{
        @Override
        public void run() {
            super.run();
            try{
            sleep(70*1000);    
            }catch(Exception e){
                e.getMessage();
            }
        }
    }

參考資料 : 來源
參考資料 : 來源2

歡迎轉載,請註明出處。

Android-Servcie killed 背景服務被回收

1 . 在Service 的 onDestory 重啟
2 . 在Service 的 onStartCommand 返回 START_STICKY

2015年4月21日 星期二

Java-基本型別轉換(String、Int、Double...等)

輸出為(String)

目標
來源
方法
String
Boolean 
String.valueOf(boolean  b)
String
Char 
String.valueOf(char  c)
String
Char[]
String.valueOf(char[]  data) 
String
Double 
String.valueOf(double  d)
String
Float 
String.valueOf(float  f)
String
Int 
String.valueOf(int  i)
String
Long 
String.valueOf(long  l)
String
Object 
String.valueOf(Object ob j)


---------------------------------------------------------------------------
輸入為(String)

目標
來源
方法
Byte(byte)
String
Byte.parseByte(String  str)
Double(double)
String
Double.parseDouble(String  str)
Float(float)
String
Float.parseFloat(String  str)
Integer(int)
String
Integer.parseInt(String  str)
Long(long)
String
Long.parseLong(String  str)


在轉換的過程容易發生意外,最好配合try catch

try{ 
    String str = "5874"; 
    int a = Integer.parseInt(str);
    
} catch (NumberFormatException e){ 

}


參考資料 : 位置
歡迎轉載,請註明出處。