如果GPS,WIFI,BLUETOOTH为ON,请更改图像视图

时间:2012-05-25 05:42:09

标签: android gps bluetooth

我有GPS,WIFI,BLUETOOTH的3个图像视图,如果GPS,WIFI,BLUETOOTH打开(编程),我想更改每个图像视图的背景图像

如果GPS,BLUETOOTH开启则图像应该更改,否则

我试过但它不起作用

final LocationManager locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);

   gpsimg.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            //Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
            //showGPSDisabledAlertToUser();
            Intent myIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(myIntent);

            gpsimg.setImageResource(R.drawable.gps);

            }else if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                gpsimg.setImageResource(R.drawable.gps);
            }

    }
});

蓝牙代码在这里

     mBtAdapter = BluetoothAdapter.getDefaultAdapter();

    if(!mBtAdapter.isEnabled())
    {

        bluetoothimg.setImageResource(R.drawable.bt_grey);
         Log.i(TAG ,"BLUETOOTH DISABLED") ;

    } 
            bluetoothimg.setOnClickListener(new View. OnClickListener() {

        public void onClick(View v) {
        // TODO Auto-generated method stub
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            int REQUEST_ENABLE_BT = 1;
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT );
             bluetoothimg.setImageResource(R.drawable.bt);
        }
    });

2 个答案:

答案 0 :(得分:1)

在致电setImageResourcestartActivityForResult之前使用startActivity: 对于GPS:

gpsimg.setImageResource(R.drawable.gps);
startActivity(myIntent);

和蓝牙:

 bluetoothimg.setImageResource(R.drawable.bt);    
 startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT );

答案 1 :(得分:0)

试试这个:

bluetoothimg.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.bt));