我想在主要活动中调用ProximitySensor活动,我已经尝试过:

Intent intentSensor = new Intent(context, ProximitySensor.class);
startActivity(intentSensor);


但是,我有一个错误:

重复的类:ProximitySensor

无法访问ProximitySensor

这是我的ProximitySensor.java:

    import org.qtproject.qt5.android.bindings.QtActivity;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.widget.Toast;
    import android.os.PowerManager;
    import android.view.WindowManager;
    import android.view.WindowManager.LayoutParams;

    public class ProximitySensor extends Activity implements SensorEventListener{
     //SensorManager lets you access the device's sensors
     //declare Variables
     private SensorManager sensorManager;

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

      //create instance of sensor manager and get system service to interact with Sensor
      sensorManager= (SensorManager)getSystemService(Context.SENSOR_SERVICE);
      WindowManager.LayoutParams params = getWindow().getAttributes();
      Sensor proximitySensor= sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
         .
         .
         .
     // called when sensor value have changed
     @Override
     public void onSensorChanged(SensorEvent event)
     {
         WindowManager.LayoutParams params = this.getWindow().getAttributes();
      if(event.sensor.getType()==Sensor.TYPE_PROXIMITY)
      {

          if(event.values[0]==0)
          {
            params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
            params.screenBrightness = 0;
            getWindow().setAttributes(params);
          }
          else
          {
            params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
            params.screenBrightness = -1f;
            getWindow().setAttributes(params);
          }
      }
     }
    }


我的主要行为是:

    .
    .
public class CustomMainActivity extends QtActivity
{
    .
    .
    .

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Context context = getApplicationContext();
        Intent intentSensor = new Intent(context, ProximitySensor.class);
        startActivity(intentSensor);


第一个问题:如何解决?
第二个问题:我可以在主要活动中仅使用onSensorChanged吗?

最佳答案

尝试将您的类名称更改为myProximitySensor,错误提示该类重复。

关于java - 主 Activity 中的接近传感器 Activity -重复类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30300325/

10-10 17:14