I/Choreographer:跳过了439帧!应用程序可能在其主线程上做了太多的工作



我想将bT模块连接到我的android手机。但是当我运行我的代码时,这个错误来了,我/编舞:跳过了439帧!应用程序可能在其主线程上做了太多的工作。

package com.example.hp.classcircuit;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
//decleare valiables
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mDevice;
ListView listView;
ConnectThread mConnectThread;
ArrayList pdal;
String DEVICE_ADDRESS;
boolean found=false;
BluetoothManager BM;
Set<BluetoothDevice> pairedDevices;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)//for chacking sutable 
//API
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listview);//Listview in xml
//call BT Manager
BM= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
//call BT Adaptor
mBluetoothAdapter=BM.getAdapter();
//call paired device
pairedDevices = mBluetoothAdapter.getBondedDevices();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(MainActivity.this, "Device not support bluetooth", Toast.LENGTH_LONG).show();
}
//check BT is on or off
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
pdal=new ArrayList();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
pdal.add(device.getName());
}
ArrayAdapter pda=new ArrayAdapter(this, android.R.layout.simple_list_item_1,pdal);
listView.setAdapter(pda);
}
listView.setOnItemClickListener(myListClickListener);

}

**

//操作点击列表查看的列表

private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String DeviceName=pdal.get(i).toString();
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals(DeviceName)) {
DEVICE_ADDRESS=device.getAddress();
Log.i(device.getName(), DEVICE_ADDRESS);
mDevice=device;
found=true;
break;
}
}
mConnectThread = new ConnectThread(mDevice);
//call the inner class method
mConnectThread.doInBackground();
}
};

//内部类

public class ConnectThread extends AsyncTask<String, Integer, Long>  {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
//constructor of inner class
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
//override the methode
protected Long doInBackground(String... strings) {
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
Log.i("Status","connected");
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) { }
return null;
}
return null;
}
//function for close the socket
public void close() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}

}

上面是我的异步类。我没有发现错误,请指导我哪里出错了。感谢所有

不要调用

mConnectThread.doInBackground();

您所做的只是破坏了AsyncTask的概念,并在UI线程上完成了所有工作。

线程规则

这个类必须遵循一些线程规则正常工作:

  • 必须在UI线程上加载AsyncTask类。这是自JELLY_BEAN起自动完成的
  • 必须在UI线程上创建任务实例
  • 必须在UI线程上调用execute(Params…)
  • 不要手动调用onPreExecute()、onPostExecute(Result)、doInBackground(Params…)、onProgressUpdate(Progress…)
  • 任务只能执行一次(如果尝试第二次执行,则会引发异常。)

AsyncTask必须使用execute()调用。因此,将上面的行更改为:

mConnectThread.execute();

请注意,您定义ConnectThread的方式需要一些String参数-extends AsyncTask<String, Integer, Long>,但您从未在代码中使用过此类参数。这些参数可以作为参数在execute(String ... params)方法中传递。最好将签名更改为:

public class ConnectThread extends AsyncTask<Void, Integer, Long> {
protected Long doInBackground(Void... params) {
//your code here
}
}

最新更新