張萌&韓墨羽——SurfaceView使用

SurfaceView使用

要求

調用系統相機實施自拍並保存到本地
調用系統攝像機,錄製音視頻並保存到本地
調用系統瀏覽器,完成搜索java
打開藍牙,顯示搜索到附近的藍牙列表
截圖藍牙列表,保存到本地
獲取本機藍牙信息,保存到txt
實現關閉藍牙
實現一鍵撥打10086

效果

在這裏插入圖片描述

代碼

package com.example.uri_use;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private ListView listId;
    BluetoothAdapter adapter;
    private List<BluetoothDevice> devices=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listId = findViewById(R.id.list_id);

        MyReceive myReceive = new MyReceive();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(myReceive,intentFilter);


        String[] s = {
                Manifest.permission.INTERNET,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA
        };
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            requestPermissions(s,100);
        }

        BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        adapter = manager.getAdapter();
    }

    public void Zipai(View view) {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        File path = new File(Environment.getExternalStorageDirectory() + "/11254698.png");
        Uri uriForFile = FileProvider.getUriForFile(this, "com.example.uri_use.fileProvider", path);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,uriForFile);
        startActivity(intent);
    }

    public void Video(View view) {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
        startActivity(intent);
    }

    public void Inter(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri parse = Uri.parse("https://www.baidu.com");
        intent.setData(parse);
        startActivity(intent);
    }

    public void bluetooth(View view) {
        Intent intent = new Intent();
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        startActivity(intent);
    }

    public void select(View view) {
        adapter.startDiscovery();
    }

    class MyReceive extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String name = device.getName();
                if (name!=null){
                    Log.i(TAG, "onReceive: "+name);
                    devices.add(device);
                }
            }else if(intent.getAction().equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
                MyAdapter myAdapter = new MyAdapter();
                listId.setAdapter(myAdapter);
                Log.i(TAG, "onReceive: 搜索完成");
                myAdapter.notifyDataSetChanged();
            }
        }
    }
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return devices.size();
        }

        @Override
        public Object getItem(int position) {
            return devices.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            HodelView hodelView;
            if (convertView==null){
                convertView= LayoutInflater.from(MainActivity.this).inflate(R.layout.bluetooths,null);
                hodelView=new HodelView();
                hodelView.name=convertView.findViewById(R.id.blue_name);
                convertView.setTag(hodelView);
            }else{
                hodelView = (HodelView) convertView.getTag();
            }
            hodelView.name.setText(devices.get(position).getName());
            return convertView;
        }
        class HodelView{
            TextView name;
        }
    }

    public void Jpic(View view) {
        View decorView = getWindow().getDecorView();
        decorView.setDrawingCacheEnabled(true);
        Bitmap bitmap = decorView.getDrawingCache();
        try {
            bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(Environment.getExternalStorageDirectory()+"/11254699.png"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void Name(View view) {
        StringBuilder sb = new StringBuilder();
        String name = adapter.getName();
        String address = adapter.getAddress();

        sb.append(name+"-"+address+"\n");
        String string = sb.toString();
        try {
            FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/11254670.txt");
            out.write(string.getBytes());
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void Close(View view) {
        adapter.disable();
    }

    public void Open_phone(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        String phone="tel:"+"13064829676";
        Uri parse = Uri.parse(phone);
        intent.setData(parse);
        startActivity(intent);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章