android.hardware.camera2
下面的代碼可以打開相機
package com.kankanla.e560.m180701_camera;
import android.annotation.SuppressLint;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mCamera();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@SuppressLint("MissingPermission")
protected void mCamera() throws CameraAccessException {
SurfaceView surfaceView = findViewById(R.id.surfaceView);
final Surface surface = surfaceView.getHolder().getSurface();
SurfaceHolder surfaceHolder = surfaceView.getHolder();
final CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cameraManager.openCamera("0", new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull final CameraDevice camera) {
try {
camera.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
try {
CaptureRequest.Builder builder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(surface);
CaptureRequest captureRequest = builder.build();
session.setRepeatingRequest(captureRequest, new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
super.onCaptureStarted(session, request, timestamp, frameNumber);
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
}
2018年7月1日日曜日
2018年4月21日土曜日
為Fragment添加一個接口,是能在MainActivity下找到Fragment下的子控件View
為Fragment添加一個接口,是能在MainActivity下找到Fragment下的子控件View
public class F1 extends Fragment {
private View view;
public Find_button find_button;
public void getFind_button(Find_button find_button) {
this.find_button = find_button;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.f1, null);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
find_button.button(view);
}
}
interface Find_button {
void button(View view);
}
-----------------------------------
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addf1();
}
protected void addf1() {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
F1 f1 = new F1();
fragmentTransaction.add(R.id.f1, f1, "f1");
fragmentTransaction.commit();
f1.getFind_button(new Find_button() {
@Override
public void button(View view) {
final Button button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.setText("F1.Button");
Toast.makeText(MainActivity.this, "F1.Button", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
2018年4月5日木曜日
Creating a Custom Toast View
Creating a Custom Toast View
创建自定义Toast视图
创建自定义Toast视图
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.toastlayout ,null);
TextView textView = view.findViewById(R.id.tx);
textView.setText("Creating a Custom Toast View");
Toast toast = new Toast(getContext());
toast.setView(view);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@android:drawable/btn_dialog" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tx"/>
</LinearLayout>
2017年11月23日木曜日
android adb 複数TCPIP链接
android adb 複数TCPIP链接
C:\> adb -s cc1394de tcpip 5555
restarting in TCP mode port: 5555
C:\> adb connect 192.168.11.111:5555
connected to 192.168.11.111:5555
C:\> adb devices
List of devices attached
192.168.11.111:5555 device
192.168.11.109:5556 device
C:\> adb -s cc1394de tcpip 5555
restarting in TCP mode port: 5555
C:\> adb connect 192.168.11.111:5555
connected to 192.168.11.111:5555
C:\> adb devices
List of devices attached
192.168.11.111:5555 device
192.168.11.109:5556 device
2017年11月17日金曜日
BroadcastReceiver 监听
protected void bttest() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:
int x = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 99);
if (x == BluetoothAdapter.STATE_CONNECTED) {
System.out.println("--------------2222222222222222------4-----");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getAddress());
System.out.println(device.getName());
System.out.println(device.getBondState());
System.out.println(device.getBluetoothClass());
}
break;
case BluetoothAdapter.ACTION_STATE_CHANGED:
//Bluetooth has been turned on or off.
System.out.println("---------ACTION_STATE_CHANGED---------------------------------------------");
System.out.println(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 22));
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
System.out.println("--------本地蓝牙适配器已完成设备发现过程。----------------");
break;
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
System.out.println("-------------本地蓝牙适配器已启动远程设备发现过程。------------");
break;
case BluetoothDevice.ACTION_FOUND:
System.out.println("---------------------发现远程设备。---------------------");
System.out.println(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(bluetoothDevice.getName());
System.out.println(bluetoothDevice.getAddress());
break;
}
}
};
IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
IntentFilter intentFilter1 = new IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
IntentFilter intentFilter2 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
IntentFilter intentFilter3 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
IntentFilter intentFilter4 = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(broadcastReceiver, intentFilter);
registerReceiver(broadcastReceiver, intentFilter1);
registerReceiver(broadcastReceiver, intentFilter2);
registerReceiver(broadcastReceiver, intentFilter3);
registerReceiver(broadcastReceiver, intentFilter4);
}
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:
int x = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 99);
if (x == BluetoothAdapter.STATE_CONNECTED) {
System.out.println("--------------2222222222222222------4-----");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getAddress());
System.out.println(device.getName());
System.out.println(device.getBondState());
System.out.println(device.getBluetoothClass());
}
break;
case BluetoothAdapter.ACTION_STATE_CHANGED:
//Bluetooth has been turned on or off.
System.out.println("---------ACTION_STATE_CHANGED---------------------------------------------");
System.out.println(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 22));
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
System.out.println("--------本地蓝牙适配器已完成设备发现过程。----------------");
break;
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
System.out.println("-------------本地蓝牙适配器已启动远程设备发现过程。------------");
break;
case BluetoothDevice.ACTION_FOUND:
System.out.println("---------------------发现远程设备。---------------------");
System.out.println(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(bluetoothDevice.getName());
System.out.println(bluetoothDevice.getAddress());
break;
}
}
};
IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
IntentFilter intentFilter1 = new IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
IntentFilter intentFilter2 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
IntentFilter intentFilter3 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
IntentFilter intentFilter4 = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(broadcastReceiver, intentFilter);
registerReceiver(broadcastReceiver, intentFilter1);
registerReceiver(broadcastReceiver, intentFilter2);
registerReceiver(broadcastReceiver, intentFilter3);
registerReceiver(broadcastReceiver, intentFilter4);
}
2017年11月12日日曜日
WifiManager.NETWORK_STATE_CHANGED_ACTION
监听WIFI
protected void t1() {
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("-----------------------------------3---------------");
System.out.println(intent.getAction());
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
Get_IP();
To_Notification_Wifi();
}
}
}
};
IntentFilter intentFilter = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
}
protected void t1() {
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("-----------------------------------3---------------");
System.out.println(intent.getAction());
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
Get_IP();
To_Notification_Wifi();
}
}
}
};
IntentFilter intentFilter = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
}
2017年11月5日日曜日
Wifi 一覧
Wifi 一覧
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
protected void show_wifi_ssid() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
List<ScanResult> scanResults = wifiManager.getScanResults();
for (ScanResult x : scanResults) {
System.out.println("--------------------------------------------");
System.out.println(x.toString());
System.out.println(x.BSSID);
System.out.println(x.SSID);
System.out.println(x.capabilities);
}
} else {
Toast.makeText(this, "WifiNull", Toast.LENGTH_SHORT).show();
}
}
I/System.out: SSID: AirPort61998, BSSID: 34:76:c5:5e:f2:2e, capabilities: [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS], level: -87, frequency: 2432, timestamp: 413176363535, distance: ?(cm), distanceSd: ?(cm), wpsState :configured, wpsDeviceName :I-O DATA 802.11n AP Router
I/System.out: 34:76:c5:5e:f2:2e
I/System.out: AirPort61998
I/System.out: [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
protected void show_wifi_ssid() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
List<ScanResult> scanResults = wifiManager.getScanResults();
for (ScanResult x : scanResults) {
System.out.println("--------------------------------------------");
System.out.println(x.toString());
System.out.println(x.BSSID);
System.out.println(x.SSID);
System.out.println(x.capabilities);
}
} else {
Toast.makeText(this, "WifiNull", Toast.LENGTH_SHORT).show();
}
}
I/System.out: SSID: AirPort61998, BSSID: 34:76:c5:5e:f2:2e, capabilities: [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS], level: -87, frequency: 2432, timestamp: 413176363535, distance: ?(cm), distanceSd: ?(cm), wpsState :configured, wpsDeviceName :I-O DATA 802.11n AP Router
I/System.out: 34:76:c5:5e:f2:2e
I/System.out: AirPort61998
I/System.out: [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]
登録:
投稿 (Atom)