1

每次 Settings.System 出现在它刚才所说的代码中时,我都会收到错误System cannot be resolved消息not a field。我怎样才能解决这个问题?

public class InternetSpeedup extends Activity {

ImageView toggle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_internet_speedup);
    toggle = (ImageView) findViewById(R.id.imageView1);

    new CountDownTimer(31000, 1000) {
        public void onTick(long millisUntilFinished) {
            //toggle.setText("You have "+ millisUntilFinished / 1000 + " seconds left");
        }
        public void onFinish() {
            // read the airplane mode setting
            boolean isEnabled = Settings.System.getInt(
                  getContentResolver(), 
                  Settings.System.AIRPLANE_MODE_ON, 0) == 1;

            // toggle airplane mode
            Settings.System.putInt(
                  getContentResolver(),
                  Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

            // Post an intent to reload
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", !isEnabled);
            sendBroadcast(intent);
        }
    };
  }    
 }
4

2 回答 2

2

here before every settings use android.provider like this :

public class MainActivity extends Activity implements OnClickListener {

    Button air;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        air = (Button) findViewById(R.id.button1);
        air.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        // read the airplane mode setting
        boolean isEnabled = android.provider.Settings.System.getInt(
              getContentResolver(), 
              android.provider.Settings.System.AIRPLANE_MODE_ON, 0) == 1;

        // toggle airplane mode
        android.provider.Settings.System.putInt(
              getContentResolver(),
              android.provider.Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

        // Post an intent to reload
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", !isEnabled);
        sendBroadcast(intent);

    }

}
于 2014-03-18T23:21:38.377 回答
2

添加import android.provider.Settings到 Java 文件顶部的导入列表中。或者,如果您使用的是 Eclipse,请尝试 [Ctrl]-[Shift]-[O]。

于 2012-08-12T10:18:21.667 回答