2015年12月20日 星期日

Android Bluetooth Transfer Data 藍芽 傳送 檔案

藍芽傳送檔案,須先準備兩台並配對好藍芽
1.AndroidManifest.xml 加上權限


    
    
    

    
        
            
                

                
            
        
    




2./res/layout/activity_main.xml 放個Button


    



3.MainActivity.java
package com.terryyamg.bluetoothdatatransfertest;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final int DISCOVER_DURATION = 300;
    private static final int REQUEST_BLU = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btSend = (Button) findViewById(R.id.btSend);
        btSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendViaBluetooth(v);
            }
        });
    }

    public void sendViaBluetooth(View v) {

        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

        if (btAdapter == null) {
            Toast.makeText(this, "裝置沒有藍芽", Toast.LENGTH_LONG).show();
        } else {
            enableBluetooth();
        }
    }

    //啟動藍芽
    public void enableBluetooth() {

        Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        //開啟藍芽時間
        discoveryIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVER_DURATION);

        startActivityForResult(discoveryIntent, REQUEST_BLU);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == DISCOVER_DURATION && requestCode == REQUEST_BLU) {

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/plain");//檔案類型
            File file = new File(Environment.getExternalStorageDirectory(), "BluetoothTest.txt"); //建立傳送檔案名稱

            String content = "Hello"; //文件內容
            try {
                FileOutputStream fop = new FileOutputStream(file);

                if (!file.exists()) { // 如果檔案不存在,建立檔案
                    file.createNewFile();
                }
                byte[] contentInBytes = content.getBytes();// 取的字串內容bytes
                fop.write(contentInBytes); // 輸出
                fop.flush();
                fop.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));

            PackageManager pm = getPackageManager();
            List<ResolveInfo> appsList = pm.queryIntentActivities(intent, 0);
            if (appsList.size() > 0) {
                String packageName = null;
                String className = null;
                boolean found = false;

                for (ResolveInfo info : appsList) {
                    packageName = info.activityInfo.packageName;
                    if (packageName.equals("com.android.bluetooth")) {
                        className = info.activityInfo.name;
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    Toast.makeText(this, "沒有找到藍芽", Toast.LENGTH_LONG).show();
                } else {
                    intent.setClassName(packageName, className);
                    startActivity(intent);
                }
            }
        } else {
            Toast.makeText(this, "取消", Toast.LENGTH_LONG).show();
        }
    }
}




檔案下載:
 https://github.com/terryyamg/BluetoothDataTransferTest
參考連結:
https://www.youtube.com/watch?v=6hQ87u9v7SY

2015年12月16日 星期三

Android 5.0 Lollipop Notification Lock Screen Visibility 鎖頻 通知

Android 5.0 Lollipop 通知訊息在鎖頻時,可以設定公開或是私密訊息

1.先設定手機
設定 ->安全性 -> 螢幕鎖定方式 ->選擇圖形、PIN碼或密碼 ->設定密碼完成 ->選擇隱藏敏感通知內容 ->完成

2./res/layout/activity_main.xml


    

3.MainActivity.java
package com.terryyamg.lollipopnotificationtest;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btNotification = (Button) findViewById(R.id.btNotification);

        btNotification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Detail.class); //點選後動作
                PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, (int) System.currentTimeMillis(), intent, 0);
                // addAction(int icon, CharSequence title, PendingIntent intent) Deprecated 的解決方法
                NotificationCompat.Action detailAction =
                        new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Detail", pIntent).build();

                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setContentTitle("標題");
                builder.setContentText("內容");
                builder.setOngoing(false); //固定
//              builder.setFullScreenIntent(pIntent,true);//一直顯示在最上層 優先於setPriority
                builder.setPriority(Notification.PRIORITY_DEFAULT); //通知的位置 - Android 5.0 PRIORITY_MIN在底線以下
                builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE); //顯示訊息-VISIBILITY_PUBLIC 公開,VISIBILITY_PRIVATE私密
                builder.addAction(detailAction); //加入下方按鈕

                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(0, builder.build());

            }
        });

    }
}
4./res/layout/detail.xml


    


5.Detail.java
package com.terryyamg.lollipopnotificationtest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Detail extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);

        TextView tvDetail = (TextView)findViewById(R.id.tvDetail);
        tvDetail.setText("跳進來了");
    }
}

設定為 VISIBILITY_PUBLIC時
設定為 VISIBILITY_PRIVATE時

檔案下載:
https://github.com/terryyamg/LollipopNotificationTest
參考連結
http://www.intertech.com/Blog/android-development-tutorial-lollipop-notifications/
https://developer.getpebble.com/blog/2014/12/19/Leverage-Android-Actionable-Notifications/
http://stackoverflow.com/questions/13808939/jelly-bean-the-method-setpriorityint-is-undefined-for-the-type-notification

2015年12月15日 星期二

Android Address 取得地址

以座標來取得住址位置
http://developer.android.com/reference/android/location/Address.html

1./res/layout/activity_main.xml 放個TextView


    



2.MainAcitvity.java
package com.terryyamg.addressestest;

import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.TextView;

import java.io.IOException;
import java.util.List;

public class MainActivity extends Activity {

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

        TextView tvAddress = (TextView) findViewById(R.id.tvAddress);

        Geocoder geocoder = new Geocoder(this);
        List<Address> addresses = null;
        double lat = 22.6721740;
        double lon = 120.3100350;
        try {
            addresses = geocoder.getFromLocation(lat, lon, 1); //放入座標
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (addresses != null && addresses.size() > 0) {
            Address address = addresses.get(0);
            String addressText = String.format("%s-%s%s%s%s",
                    address.getCountryName(), //國家
                    address.getAdminArea(), //城市
                    address.getLocality(), //區
                    address.getThoroughfare(), //路
                    address.getSubThoroughfare() //巷號
            );

            tvAddress.setText(addressText);
        }

    }

}


檔案下載:
https://github.com/terryyamg/AddressesTest
參考連結:
http://developer.android.com/reference/android/location/Address.html
http://wptrafficanalyzer.in/blog/android-reverse-geocoding-at-touched-location-in-google-map-android-api-v2/

2015年12月9日 星期三

Android MVP 設計模式 Spinner練習

練習MVP設計模式,以Spinner為例
將檔案切成Model View Presenter


1.在Layout放置一個Spinner元件
2.View - MainActivity.java
package com.terryyamg.mvptest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

//View
public class MainActivity extends Activity implements MainActivityView {

    private Spinner spinner;
    private ArrayAdapter<String> listAdapter;

    private MainActivityPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = (Spinner) findViewById(R.id.spinner);

        presenter = new MainActivityPresenterImpl(this);
        presenter.guide(); //導向
    }

    //Override MainActivityView 設定Adapter
    @Override
    public void setAdapter(String[] data) {
        listAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, data);
        listAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    //Override MainActivityView 設定Spinner動作
    @Override
    public void setSpinner(final String[] data) {
        spinner.setAdapter(listAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int pos1, long arg3) {
                Toast.makeText(MainActivity.this, data[pos1], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });
    }
}

3.View - MainActivityView.java
package com.terryyamg.mvptest;

public interface MainActivityView {

    void setAdapter(String[] data);

    void setSpinner(String[] data);

}

4.Presenter - MainActivityPresenter.java
package com.terryyamg.mvptest;

public interface MainActivityPresenter {

    void guide();

}

5.Presenter - MainActivityPresenterImpl.java
package com.terryyamg.mvptest;

//Presenter
public class MainActivityPresenterImpl implements MainActivityPresenter {

    private MainActivityView mView;
    private MainActivityModel mModel;

    public MainActivityPresenterImpl(MainActivityView mView) {
        this.mView = mView;
        this.mModel = new MainActivityModelImpl();
    }


    @Override
    public void guide() {
        mModel.setData(mView); //導向Model
    }
}

6.Model - MainActivityModel.java
package com.terryyamg.mvptest;

public interface MainActivityModel {

    void setData(MainActivityView view);

}

7.Model - MainActivityModelImpl.java
package com.terryyamg.mvptest;

//Model
public class MainActivityModelImpl implements MainActivityModel {

    @Override
    public void setData(MainActivityView view) {
        String[] data = {"A", "B", "C", "D", "E"};

        //放入資訊
        view.setAdapter(data);
        view.setSpinner(data);
    }
}

完成後就可以得到一個與一般的spinner相同的效果

檔案下載:
 https://github.com/terryyamg/MVPTest
參考連結:
 https://github.com/antoniolg/androidmvp