Chinaunix首页 | 论坛 | 博客
  • 博客访问: 257133
  • 博文数量: 69
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 900
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-07 16:35
文章分类

全部博文(69)

文章存档

2011年(1)

2010年(68)

我的朋友

分类: LINUX

2010-08-10 22:15:14

  2010年07月15日 20:14  收藏本页

  Android 屏幕显示设置

  程序中默的显示是带有标题栏和系统信息栏的,有的时候,这很影响程序界面的美观。手机默认的是竖屏,或与感应器状态相关,为了某种效果,我们的程序需要限制使用横屏或竖屏。以下的代码就解决了上述问题。

  //设置为无标题栏

  Java代码

  requestWindowFeature(Window.FEATURE_NO_TITLE);

  requestWindowFeature(Window.FEATURE_NO_TITLE);

  //设置为全屏模式

  Java代码

  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

  //设置为横屏

  Java代码

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

  Android Intent参数传递

  当Activity与Activity/Service(或其它情况)有时与要进行参数传递,最常用也是最简单的方式就是通过Intent来处理。

  看如下代码:

  Java代码

  Intent intent = new Intent(...);

  Bundle bundle = new Bundle();

  bundle.putString("NAME", "zixuan");

  intent.putExtras(bundle);

  context.startActivity(intent); 或 context.startService(intent);

  Intent intent = new Intent(...);

  Bundle bundle = new Bundle();

  bundle.putString("NAME", "zixuan");

  intent.putExtras(bundle);

  context.startActivity(intent); 或 context.startService(intent);

  当然,有传送就有接收,接收也很简单,如:

  Java代码

  Bundle bunde = intent.getExtras();

  String name = bunde.getInt("NAME");

  Bundle bunde = intent.getExtras();

  String name = bunde.getInt("NAME");

  当然参数KEY要与传送时的参数一致。

  Android 获取手机号/手机串号

  在j2me中,根本没有办法获取用户的手机号码,就连获取手机串号(IMEI)都基本上无法实现,然后在android手机上一切都是如此的简单,看代码:

Java代码

  TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

  String imei = tm.getDeviceId();

  String tel = tm.getLine1Number();

  TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

  String imei = tm.getDeviceId();

  String tel = tm.getLine1Number();

  看来,android的确加速了j2me的消亡。

  Android 振动器

  总感觉手机上的振动器没有多大用处(当然静音模式下的振铃很有用),但还是顺带着说一下吧,只有两行代码:

  1、获取振动服务的实例

  Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

  2、设置振动时长,单位当然也是ms

  vibrator.vibrate(1000);

  如果你觉得这样过去单调的话,可以设个节奏:

  vibrator.vibrate(new long[]{10, 100, 20, 200}, -1);

  两个参数,习惯告诉我第一个是节奏,第二个是重复次数,可事实并没有这么简单,我翻译不好,大家还是看原文吧:

  Java代码

  public void vibrate (long[] pattern, int repeat)

  pattern: an array of longs of times to turn the vibrator on or off.

  repeat: the index into pattern at which to repeat, or -1 if you don't want to repeat.

  public void vibrate (long[] pattern, int repeat)

  pattern: an array of longs of times to turn the vibrator on or off.

  repeat: the index into pattern at which to repeat, or -1 if you don't want to repeat.

  google喜欢弄些技巧,我却觉得这里有点弄巧成拙了。

  Android 闹钟

  最近看了一下Android的闹钟管理类(AlarmManager),真不错误,强大又简单,代码如下:

  1、建立一个AlarmReceiver继承入BroadcastReceiver,并在AndroidManifest.xml声明

  Java代码

  public static class AlarmReceiver extends BroadcastReceiver {

  @Override

  public void onReceive(Context context, Intent intent) {

  Toast.makeText(context, "闹钟提示:时间到!", Toast.LENGTH_LONG).show();

}

  }

  public static class AlarmReceiver extends BroadcastReceiver {

  @Override

  public void onReceive(Context context, Intent intent) {

  Toast.makeText(context, "闹钟提示:时间到!", Toast.LENGTH_LONG).show();

  }

  }

  2、建立Intent和PendingIntent,来调用目标组件。

  Java代码

  Intent intent = new Intent(this, AlarmReceiver.class);

  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

  Intent intent = new Intent(this, AlarmReceiver.class);

  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

  3、设置闹钟

  获取闹钟管理的实例:

  Java代码

  AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

  AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

  设置单次闹钟:

  Java代码

  alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5*1000), pendingIntent);

  alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5*1000), pendingIntent);

  设置周期闹钟:

  Java代码

  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10*1000), (24*60*60*1000), pendingIntent);

  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10*1000), (24*60*60*1000), pendingIntent);

  搞定!当然这里闹钟的响应处理只是用的文字,你可以播放声音,或都用更复杂的一系统通知,在这里你就是上帝,一切由你做主。

阅读(2495) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~