我正在尝试使用BluetoothAdapter BA
Intent方法将putExtra(String str, Bundle bundle)
传递给另一个活动,但我的Android工作室的编译器显示错误。当我将鼠标悬停在方法下出现的红色曲线上时,它会显示我
Can't resolve method 'putExtra(java.lang.String, android.bluetooth.BluetoothAdapter)'
如果我理解正确,Bundle基本上是任何对象,因此通过putExtra将BluetoothAdapter传递给另一个活动不会有任何问题。
这是我的 MainActivity.java
package vertex2016.mvjce.edu.bluealert;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import static java.lang.Thread.sleep;
public class MainActivity extends AppCompatActivity {
public BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();
private int REQ_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void connect(View v)
{
if(BA == null)
Toast.makeText(MainActivity.this, "System Doesn't Support Bluetooth", Toast.LENGTH_SHORT).show();
else if(!BA.isEnabled())
{
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT, REQ_CODE);
}
else {
Toast.makeText(MainActivity.this, "ALREADY ON!!", Toast.LENGTH_SHORT).show();
searchBTDevices();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!=RESULT_CANCELED) {
Toast.makeText(MainActivity.this, "TURNED ON!", Toast.LENGTH_SHORT).show();
searchBTDevices();
}
else
Toast.makeText(MainActivity.this,"FAILED TO ENABLE BLUETOOTH", Toast.LENGTH_LONG).show();
}
public void searchBTDevices()
{
Thread searchThread = new Thread() {
@Override
public void run() {
Intent searchBT = new Intent(getApplicationContext(), SearchBTDevice.class);
searchBT.putExtra("BT_ADAPTER", BA);
startActivity(searchBT);
}
};
searchThread.start();
}
}
这是我应该收到额外信息的 SearchBTDevices.java 。
package vertex2016.mvjce.edu.bluealert;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class SearchBTDevice extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_btdevice);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
我哪里错了?
感谢您的时间!!
答案 0 :(得分:0)
Intent只能用于传递基元Parcelable
和Serializable
对象。 BluetoothAdapter
不支持其中任何一项,也无法使用原语构建,因此您无法创建可帮助您的Parcelable。
因此,当遇到这种情况时,您需要传入Activty可用于重新构建或检索执行所需对象的参数。
如果您需要能够使用不同的BluetoothAdapter,那么您需要告诉SearchBTDevices
要检查哪个BluetoothAdapter并让SearchBTDevices
检索适配器本身。例如,
public class SearchBTDevices extends Activity {
public static final String PARAM_ADAPTER_TYPE = "adapterType";
public static final int ADAPTER_DEFAULT = 1;
public static final int ADAPTER_ALTERNATE = 2;
private BluetoothAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int adapterType = getIntent().getIntExtra(PARAM_ADAPTER_TYPE, ADAPTER_DEFAULT);
switch(adapterType) {
case PARAM_ADAPTER_ALTERNATE:
mAdapter = getAlternateAdapter(); // This is whatever method you need to get it.
break;
default:
mAdapter = getDefaultAdapter();
}
}
}
现在,无论何时需要启动此活动,只需执行以下操作:
Intent intent = new Intent(getContext(), SearchBTDevices.class);
intent.putExtra(SearchBTDevices.PARAM_ADAPTER_TYPE, SearchBTDevices.ADAPTER_ALTERNATE);
startActivity(intent);
因此,当SearchBTDevices
启动时,它将从intent中提取参数,并知道它需要使用备用适配器。