Activity常用知识

  1.获取系统语言设置

String str = Locale.getDefault().getLanguage();

系统语言改变发送广播
filter = new IntentFilter("Intent.ACTION_LOCALE_CHANGED");

  2.获得系统电量

public class LowerPowerReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        // TODO Auto-generated method stub
        if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction()))
        {
            //当前电量
            int level = intent.getIntExtra("level", 0);
            //电量总刻度
            int scale = intent.getIntExtra("scale", 100);
            //电池电量
            float a = level*100.0f/scale;
            Toast.makeText(context, "电量为:"+a+"%", Toast.LENGTH_SHORT).show();
        }
        if(Intent.ACTION_BATTERY_LOW.equals(intent.getAction()))
        {
            Toast.makeText(context, "电量过低,请及时充电", Toast.LENGTH_SHORT).show();
        }
        if(Intent.ACTION_BATTERY_OKAY.equals(intent.getAction()))
        {
            Toast.makeText(context, "电量已满,请拔出充电器", Toast.LENGTH_SHORT).show();
        }

    }

}


 3. 获得MAC

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  
private String getLocalMacAddress() {
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    return info.getMacAddress();
  }

4. 获得网络状态

 

 private boolean getNetWorkStatus() {   
        
        boolean netSataus = false;   
        ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);   
       
        cwjManager.getActiveNetworkInfo();   
       
        if (cwjManager.getActiveNetworkInfo() != null) 
        {   
        
            netSataus = cwjManager.getActiveNetworkInfo().isAvailable();   
        }   
       
        if (!netSataus) 
        {   

            Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络")
                    .setMessage("是否对网络进行设置?");
            b.setPositiveButton("是", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int whichButton)
                {
                    Intent mIntent = new Intent("/");
                    ComponentName comp = new ComponentName(
                            "com.android.settings",
                            "com.android.settings.WirelessSettings");
                    mIntent.setComponent(comp);
                    mIntent.setAction("android.intent.action.VIEW");
                    startActivityForResult(mIntent, 0);
                }
            }).setNeutralButton("否", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int whichButton)
                {
                    dialog.cancel();
                }
            }).show();
        }

        return netSataus;
    }
    

 

 

posted @ 2012-07-31 17:20  那些人追过的年  阅读(342)  评论(0编辑  收藏  举报