在Android中的另一个类中按住按钮调用方法

在Android中的另一个类中按住按钮调用方法,android,android-button,bluetooth-lowenergy,android-bluetooth,Android,Android Button,Bluetooth Lowenergy,Android Bluetooth,我是Android的新手,我正在尝试测试Android 4.3的新蓝牙LE功能。我跟踪了找到的文档。但我在实际启动代码时遇到了一个绊脚石 我想简单地用一个按钮调用我的Scanlevice代码,但我看不到任何东西可以让我简单地这么做。实际上,我所拥有的只是这门课和当你做一个新项目时他们制作的示例hello word应用程序 有人能帮我吗?我知道这很难说,但我真的被难住了 作为参考,我的蓝牙课程如下: public class DeviceScanActivity extends ListActiv

我是Android的新手,我正在尝试测试Android 4.3的新蓝牙LE功能。我跟踪了找到的文档。但我在实际启动代码时遇到了一个绊脚石

我想简单地用一个按钮调用我的Scanlevice代码,但我看不到任何东西可以让我简单地这么做。实际上,我所拥有的只是这门课和当你做一个新项目时他们制作的示例hello word应用程序

有人能帮我吗?我知道这很难说,但我真的被难住了

作为参考,我的蓝牙课程如下:

public class DeviceScanActivity extends ListActivity {

private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;

// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;

private ArrayAdapter<Object> list;
private Handler handler = new Handler();

protected void OnCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    list = new ArrayAdapter<Object>(getApplicationContext(), android.R.layout.simple_list_item_1);
    setListAdapter(list);
    scanLeDevice(true);
}

@TargetApi(18)
public void scanLeDevice(final boolean enable) {
    list.add("Scanning...");
    final BluetoothAdapter adapter = getBluetoothAdapter();
    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;

               // mBluetoothAdapter.stopLeScan(mLeScanCallback);
                adapter.stopLeScan(callback);
                list.clear();
            }
        }, SCAN_PERIOD);

        mScanning = true;
        adapter.startLeScan(callback);
    } else {
        mScanning = false;
        adapter.stopLeScan(callback);
    }

}
@TargetApi(18)
private BluetoothAdapter getBluetoothAdapter()
{
    BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    return manager.getAdapter();
}
private final BluetoothAdapter.LeScanCallback callback = new BluetoothAdapter.LeScanCallback() 
{

    @Override
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
        // TODO Auto-generated method stub

        {
            list.add("found: " + device);
            runOnUiThread(new Runnable(){
                @Override
                public void run()
                {
                    list.add(device);
                    list.notifyDataSetChanged();
                }
            });
    }
}
};
public class DeviceScanActivity扩展了ListActivity{
私人蓝牙适配器mBluetoothAdapter;
私有布尔扫描;
私人经理人;
//10秒后停止扫描。
专用静态最终长扫描周期=10000;
专用阵列适配器列表;
私有处理程序=新处理程序();
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
list=newarrayadapter(getApplicationContext(),android.R.layout.simple\u list\u item\u 1);
setListAdapter(列表);
扫描设备(真实);
}
@塔吉塔皮(18)
公共无效扫描设备(最终布尔启用){
添加(“扫描…”);
最终BluetoothAdapter=getBluetoothAdapter();
如果(启用){
//在预定义的扫描周期后停止扫描。
mHandler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
mScanning=false;
//mBluetoothAdapter.stopLeScan(mLeScanCallback);
适配器.stopLeScan(回调);
list.clear();
}
},扫描周期);
mScanning=true;
adapter.startedscan(回调);
}否则{
mScanning=false;
适配器.stopLeScan(回调);
}
}
@塔吉塔皮(18)
私有BluetoothAdapter getBluetoothAdapter()
{
BluetoothManager=(BluetoothManager)getSystemService(Context.BLUETOOTH\u服务);
返回manager.getAdapter();
}
private final BluetoothAdapter.LeScanCallback回调=新建BluetoothAdapter.LeScanCallback()
{
@凌驾
public void onLeScan(最终BluetoothDevice设备,int rssi,字节[]扫描记录){
//TODO自动生成的方法存根
{
添加(“发现:+设备”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐
{
列表。添加(设备);
list.notifyDataSetChanged();
}
});
}
}
};

}

在MainActivity中添加新按钮:

<Button
    android:id="@+id/btnCallMethod"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Call Method" />
private void addListeners() {
  Button btnCallMethod = (Button) this.findViewById(R.id.btnCallMethod);

  // CALL METHOD BUTTON
  btnCallMethod.setOnTouchListener(new OnTouchListener() {

     @Override
     public boolean onTouch(View v, MotionEvent event) {
     if (event.getAction() == MotionEvent.ACTION_UP) {
       Intent intent = new Intent(getApplicationContext(), DeviceScanActivity.class);
       startActivity(intent); // start the DeciveScanActivity
     }
     return false;
   }
 });
}
这就是addListeners()方法在MainActivity中的外观:

<Button
    android:id="@+id/btnCallMethod"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Call Method" />
private void addListeners() {
  Button btnCallMethod = (Button) this.findViewById(R.id.btnCallMethod);

  // CALL METHOD BUTTON
  btnCallMethod.setOnTouchListener(new OnTouchListener() {

     @Override
     public boolean onTouch(View v, MotionEvent event) {
     if (event.getAction() == MotionEvent.ACTION_UP) {
       Intent intent = new Intent(getApplicationContext(), DeviceScanActivity.class);
       startActivity(intent); // start the DeciveScanActivity
     }
     return false;
   }
 });
}
现在在DeviceScanActivity的onCreate()方法中调用该方法

别忘了将DeviceScanActivity添加到AndroidManifest.xml中

<activity
     android:name="com.example.example.DeviceScanActivity"
</activity>

My other class旨在扫描可用的蓝牙信号并列出它们。所以我想这是一种活动。对android来说真的很新,所以它对事物的定义仍然让我困惑。编辑了我的答案。我想这就是你要找的。我在intent调用中的DeviceScanActivity.class有一个错误。IDE说它可以解析为一个类型。你能帮忙吗?你的DeviceScanActivity在另一个包中吗?我只是在我的项目中添加了另一个类。我不知道是不是我该怎么检查?