注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

享受编码

    的乐趣

 
 
 

日志

 
 

android知识拾遗——1  

2011-05-24 20:08:48|  分类: android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1.获取mac地址

Java代码 
  1. 1、<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  2. 2private String getLocalMacAddress() {
  3. WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  4. WifiInfo info = wifi.getConnectionInfo();
  5. return info.getMacAddress();
  6. }
1、<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>    2、private String getLocalMacAddress() {      WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);      WifiInfo info = wifi.getConnectionInfo();      return info.getMacAddress();    }

2.全屏
一是在代码中设置,另一种方法是在配置文件里改!

一、在代码中设置:

Java代码
  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. //无title
  4. requestWindowFeature(Window.FEATURE_NO_TITLE);
  5. //全屏
  6. getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
  7. WindowManager.LayoutParams. FLAG_FULLSCREEN);
  8. setContentView(R.layout.main);
  9. }
  10. }
public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //无title      requestWindowFeature(Window.FEATURE_NO_TITLE);      //全屏      getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,         WindowManager.LayoutParams. FLAG_FULLSCREEN);             setContentView(R.layout.main);    }   } 

设置全屏的俩段代码必须在setContentView(R.layout.main) 之前,不然会报错。

二、在配置文件里修改(android:theme="@android:style/Theme.NoTitleBar.Fullscreen"):

3、网络状态

Java代码
  1. <uses-permission
  2. android:name="android.permission.ACCESS_NETWORK_STATE" />
  3. private boolean getNetWorkStatus() {
  4. boolean netSataus = false;
  5. ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  6. cwjManager.getActiveNetworkInfo();
  7. if (cwjManager.getActiveNetworkInfo() != null) {
  8. netSataus = cwjManager.getActiveNetworkInfo().isAvailable();
  9. }
  10. if (!netSataus) {
  11. Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络")
  12. .setMessage("是否对网络进行设置?");
  13. b.setPositiveButton("是", new DialogInterface.OnClickListener() {
  14. public void onClick(DialogInterface dialog, int whichButton) {
  15. Intent mIntent = new Intent("/");
  16. ComponentName comp = new ComponentName(
  17. "com.android.settings",
  18. "com.android.settings.WirelessSettings");
  19. mIntent.setComponent(comp);
  20. mIntent.setAction("android.intent.action.VIEW");
  21. startActivityForResult(mIntent,0);
  22. }
  23. }).setNeutralButton("否", new DialogInterface.OnClickListener() {
  24. public void onClick(DialogInterface dialog, int whichButton) {
  25. dialog.cancel();
  26. }
  27. }).show();
  28. }
  29. return netSataus;
  30. }
 <uses-permission    android:name="android.permission.ACCESS_NETWORK_STATE" />      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;      }

4 .断GPS状态

Java代码
  1. public boolean isGpsEnabled(Context context)
  2. {
  3. LocationManager locationManager =((LocationManager)context.getSystemService(Context.LOCATION_SERVICE));
  4. List<String> accessibleProviders = locationManager.getProviders(true);
  5. return accessibleProviders != null && accessibleProviders.size() > 0;
  6. }
    public boolean isGpsEnabled(Context context)      {          LocationManager locationManager =((LocationManager)context.getSystemService(Context.LOCATION_SERVICE));          List<String> accessibleProviders = locationManager.getProviders(true);          return accessibleProviders != null && accessibleProviders.size() > 0;      }   

5.禁止横屏幕 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

6.禁用重复加载
1. 修改AndroidManifest.xml文件,在activity标签下面增加参数:

Java代码
  1. < activity android:name="MyActivity"
  2. android:configChanges="orientation|keyboardHidden">
< activity android:name="MyActivity"  android:configChanges="orientation|keyboardHidden">

2.在MyActivity 中重载onConfigurationChanged(每次切屏的时候会自动调用)方法,初始化横竖屏方向不同的显示界面,以及其他数据的初始化操作:

Java代码
  1. @Override
  2. public void onConfigurationChanged(Configuration newConfig) {
  3. super.onConfigurationChanged(newConfig);
  4. if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  5. //加入横屏要处理的代码
  6. }else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  7. //加入竖屏要处理的代码
  8. }
  9. }
@Override    public void onConfigurationChanged(Configuration newConfig) {       super.onConfigurationChanged(newConfig);    if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {             //加入横屏要处理的代码    }else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {               //加入竖屏要处理的代码    }      }

7、开机启动程序

Java代码
  1. 1)public class StartupReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. Intent startupintent = new Intent(context,StrongTracks.class);
  5. startupintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  6. context.startActivity(startupintent);
  7. }
  8. }
  9. 2)<receiver
  10. android:name=".StartupReceiver">
  11. <intent-filter>
  12. <!-- 系统启动完成后会调用 -->
  13. <action
  14. android:name="android.intent.action.BOOT_COMPLETED">
  15. </action>
  16. </intent-filter>
  17. </receiver>
  评论这张
 
阅读(618)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017