gpt4 book ai didi

java - 在虚拟设备管理器中运行但在手机上强制关闭的代码

转载 作者:行者123 更新时间:2023-12-01 14:18:54 29 4
gpt4 key购买 nike

package com.example.arduino;

import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity {

private Button java;
private SeekBar servo;
byte srValue;


private static final int REQUEST_ENABLE_BT = 1;

ListView listDevicesFound;
Button btnScanDevice;
TextView stateBluetooth;
BluetoothAdapter bluetoothAdapter;

ArrayAdapter<String> btArrayAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnScanDevice = (Button)findViewById(R.id.scandevice);

stateBluetooth = (TextView)findViewById(R.id.bluetoothstate);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
listDevicesFound = (ListView)findViewById(R.id.devicesfound);
btArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);
listDevicesFound.setAdapter(btArrayAdapter);

servo = (SeekBar)findViewById(R.id.servocontroller);

CheckBlueToothState();

btnScanDevice.setOnClickListener(btnScanDeviceOnClickListener);

registerReceiver(ActionFoundReceiver,
new IntentFilter(BluetoothDevice.ACTION_FOUND));


servo.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

OutputStream Device;

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try{
srValue = (byte)progress;
}catch(Exception e)
{
e.printStackTrace();
}
}

public void onStopTrackingTouch(SeekBar seekBar) {
String temp = "r";
byte bytes[] = temp.getBytes();

try {
Device.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}

try {
Device.write(srValue);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

});
}


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(ActionFoundReceiver);
}

private void CheckBlueToothState(){
if (bluetoothAdapter == null){
stateBluetooth.setText("Bluetooth NOT support");
}else{
if (bluetoothAdapter.isEnabled()){
if(bluetoothAdapter.isDiscovering()){
stateBluetooth.setText("Bluetooth is currently in device discovery process.");
}else{
stateBluetooth.setText("Bluetooth is Enabled.");
btnScanDevice.setEnabled(true);
}
}else{
stateBluetooth.setText("Bluetooth is NOT Enabled!");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}

private Button.OnClickListener btnScanDeviceOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
btArrayAdapter.clear();
bluetoothAdapter.startDiscovery();
}};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == REQUEST_ENABLE_BT){
CheckBlueToothState();
}
}

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
btArrayAdapter.add(device.getName() + "\n" + device.getAddress());
btArrayAdapter.notifyDataSetChanged();
}
}};


}

好吧,这段代码在虚拟设备管理器中编译,但是当我将它导入到我的手机时,它立即崩溃,我已经被困了几个小时。怎么可以在eclipse上编译,但在手机上不行呢?我尝试了许多较低级别的 Android 操作系统,但每次仍然强制关闭

    07-23 21:53:40.204  6517  6517 E AndroidRuntime: FATAL EXCEPTION: main

07-23 21:53:40.204 6517 6517 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.arduino/com.example.arduino.MainActivity}: java.lang.SecurityException: Permission Denial: starting Intent { act=android.bluetooth.adapter.action.REQUEST_ENABLE cmp=com.android.settings/.bluetooth.RequestPermissionActivity } from ProcessRecord{41864c40 6517:com.example.arduino/u0a10153} (pid=6517, uid=10153) requires android.permission.BLUETOOTH

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:150)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.os.Looper.loop(Looper.java:137)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5195)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.bluetooth.adapter.action.REQUEST_ENABLE cmp=com.android.settings/.bluetooth.RequestPermissionActivity } from ProcessRecord{41864c40 6517:com.example.arduino/u0a10153} (pid=6517, uid=10153) requires android.permission.BLUETOOTH

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1425)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1379)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1893)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1412)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3370)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3331)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at com.example.arduino.MainActivity.CheckBlueToothState(MainActivity.java:121)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at com.example.arduino.MainActivity.onCreate(MainActivity.java:52)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5104)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)

07-23 21:53:40.204 6517 6517 E AndroidRuntime: ... 11 more

07-23 21:53:40.214 661 12263 W ActivityManager: Force finishing activity com.example.arduino/.MainActivity

07-23 21:53:40.324 661 12263 D dalvikvm: GC_FOR_ALLOC freed 1794K, 27% free 24899K/33868K, paused 77ms, total 78ms

07-23 21:53:40.824 661 676 W ActivityManager: Activity pause timeout for ActivityRecord{416c36f0 u0 com.example.arduino/.MainActivity}

07-23 21:53:40.965 661 661 I ActivityManager: No longer want com.android.musicfx (pid 6218): empty #17

07-23 21:53:41.505 6517 6517 I Process : Sending signal. PID: 6517 SIG: 9

07-23 21:53:41.505 661 1293 I ActivityManager: Process com.example.arduino (pid 6517) has died.

07-23 21:53:41.525 661 22882 W InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@41c3e618 attribute=null, token = android.os.BinderProxy@41c84818

07-23 21:53:42.086 873 12278 D dalvikvm: GC_FOR_ALLOC freed 6812K, 38% free 27836K/44836K, paused 60ms, total 60ms

07-23 21:53:42.146 873 12278 D dalvikvm: GC_FOR_ALLOC freed 38K, 36% free 29069K/44836K, paused 34ms, total 34ms

07-23 21:53:42.146 873 12278 I dalvikvm-heap: Grow heap (frag case) to 29.565MB for 1048592-byte allocation

07-23 21:53:42.206 873 885 D dalvikvm: GC_FOR_ALLOC freed 0K, 35% free 30093K/45864K, paused 53ms, total 53ms

07-23 21:53:42.236 873 12278 D dalvikvm: GC_FOR_ALLOC freed 1K, 35% free 30093K/45864K, paused 35ms, total 35ms

07-23 21:53:42.246 873 12278 I dalvikvm-heap: Grow heap (frag case) to 31.344MB for 1865972-byte allocation

07-23 21:53:42.286 873 885 D dalvikvm: GC_FOR_ALLOC freed <1K, 34% free 31914K/47688K, paused 38ms, total 38ms

最佳答案

请记住在您的应用 list 文件中声明蓝牙权限:

<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>

关于java - 在虚拟设备管理器中运行但在手机上强制关闭的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17824252/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com