我正在尝试实施一个必须在其区域内找到蓝牙设备的Android应用程序(蓝牙设备名称为HXMXXXXXX,X - 是一个数字的条件)。我已经完成了这一部分并且工作正常,现在我正在尝试在执行搜索设备的操作时显示进度对话框。我试图通过使用AsyncTask来做到这一点,但它不起作用。这是我的代码:
package com.yast;
import android.app.ProgressDialog;
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.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class ConnectionScreen extends ActionBarActivity {
//Declaration of components
private static final String TAG = "ConnectionScreen";
public static final String BROADCAST = "PACKAGE_NAME.android.action.broadcast";
private static final int REQUEST_ENABLE_BT = 1;
private Button buttonSearch;
private CheckBox checkBoxAutoConnect;
private ListView listOfDevices;
private TextView statusId;
private TextView statusConnection;
private BluetoothAdapter bluetoothadapt;
private Set<BluetoothDevice> pairedDevices;
private ArrayAdapter<String> deviceAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connection_screen);
linkViewToResources();
//ListView
List<String> arrayListOfDevices = new ArrayList<String>();
arrayListOfDevices.add("unu");
arrayListOfDevices.add("doi");
listOfDevices.setAdapter(deviceAdapter);
bluetoothadapt=BluetoothAdapter.getDefaultAdapter();
if(bluetoothadapt==null){
statusConnection.setText("Not supported");
Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth",Toast.LENGTH_LONG).show();
buttonSearch.setEnabled(false);
} else{
buttonSearch.setEnabled(true);
}
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
private void linkViewToResources(){
buttonSearch = (Button) findViewById(R.id.searchButton);
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
find(view);
}
});
checkBoxAutoConnect = (CheckBox) findViewById(R.id.checkBoxAutoconnect);
listOfDevices = (ListView) findViewById(R.id.listOfDevices);
deviceAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
}
final BroadcastReceiver bReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// add the name and the MAC address of the object to the arrayAdapter
// get paired devices
if (device.getName().startsWith("HXM") && device.getName().length() == 9) {
deviceAdapter.add(device.getName() + "\n" + device.getAddress());
//deviceAdapter.notifyDataSetChanged();
}
}
}
};
public void find(View view) {
if (bluetoothadapt.isDiscovering()) {
// the button is pressed when it discovers, so cancel the discovery
bluetoothadapt.cancelDiscovery();
}
else {
deviceAdapter.clear();
bluetoothadapt.startDiscovery();
new ProgressTask().execute(null, null, null);
}
}
@Override
protected void onPause() {
super.onPause();
try {
if (bReceiver != null) {
unregisterReceiver(bReceiver);
}
}
catch (IllegalArgumentException e){
Log.e(TAG, e.getMessage(), e);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.connection_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class ProgressTask extends AsyncTask<String, Void, Boolean> {
/** progress dialog to show user that the backup is processing. */
private ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(getApplicationContext());
this.dialog.setMessage("Progress start");
this.dialog.show();
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
deviceAdapter.notifyDataSetChanged();
}
protected Boolean doInBackground(final String... args) {
try{
Intent intent = new Intent(BROADCAST);
sendBroadcast(intent);
return true;
} catch (Exception e){
Log.e("tag", "error", e);
return false;
}
}
}
}
这是我得到的错误:
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:702)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
at android.app.Dialog.show(Dialog.java:277)
at com.yast.ConnectionScreen$ProgressTask.onPreExecute(ConnectionScreen.java:171)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.yast.ConnectionScreen.find(ConnectionScreen.java:122)
at com.yast.ConnectionScreen$1.onClick(ConnectionScreen.java:86)
at android.view.View.performClick(View.java:4162)
at android.view.View$PerformClick.run(View.java:17082)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
任何人都可以帮助我吗?谢谢:))
答案 0 :(得分:2)
试试这个
dialog = new ProgressDialog(ConnectionScreen.this);
而不是
dialog = new ProgressDialog(getApplicationContext());
您应该传递Activity
上下文而不是Application
上下文。