kevin33643kevin33643.blog.chinaunix.net
kevin33643
全部博文(241)
java基础(1)
php(0)
tcp/ip(1)
python学习(4)
python算法(1)
python程序(10)
软件开发(1)
IT杂文(3)
软件工程(3)
IT评论(5)
mobile评论(2)
android程序(2)
android开发(9)
程序员面试宝典(7)
你必须知道的495(1)
C专家编程(3)
kernel(4)
虚拟机(11)
script(4)
shell(6)
系统(6)
vim(19)
git(3)
grub(4)
命令(18)
使用(16)
工具(4)
linux转载(5)
通信为什么?(1)
通信转载(1)
经典算法(4)
字符串(9)
基本算法(7)
排序算法(6)
数据结构(7)
2013年(1)
2012年(8)
2011年(62)
2010年(109)
2009年(61)
yufei201
云中的二
ITchap
小雅贝贝
zhanglia
bfllinux
wb123456
梦幻阵容
黑色冷笑
yangyefe
dynamder
cynthia
浪花小雨
格伯纳
zjzy2000
分类: 嵌入式
2010-08-04 08:49:18
package com.yuanlin.battery;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.RemoteViews;import android.widget.TextView;public class ActivityMain extends Activity { private static int NOTIFICATIONS_ID = R.layout.main; NotificationManager mNotificationManager = null; RemoteViews mRemoteViews = null; Notification mNotification = null; PendingIntent mContentIntent = null; TextView textView = null; int level; int scale; private BroadcastReceiver batteryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { if (Intent.ACTION_BATTERY_CHANGED.equals(arg1.getAction())) { level = arg1.getIntExtra("level", 0); scale = arg1.getIntExtra("scale", 0); textView.setText("" + (level * 100 / scale) + "%"); notifyBatteryInfo("" + (level * 100 / scale) + "%"); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView) findViewById(R.id.textViewId); registerReceiver(batteryReceiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, Menu.FIRST + 1, 1, "Exit").setIcon( android.R.drawable.ic_menu_delete); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case (Menu.FIRST + 1): finish(); break; } return super.onOptionsItemSelected(item); } private void notifyBatteryInfo(String info) { mNotification = new Notification(); mNotification.icon = R.drawable.a_icon + (level * 100 / scale); mNotification.when = System.currentTimeMillis(); mNotification.flags |= Notification.FLAG_NO_CLEAR; mContentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ActivityMain.class), 0); mNotification.setLatestEventInfo(this, info, "yl33643@126.com", mContentIntent); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotificationManager.notify(NOTIFICATIONS_ID, mNotification); }}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textViewId" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/hello" android:gravity="center_vertical|center_horizontal" android:textSize="40dip" android:textStyle="bold" android:textColor="#00FF00" /></LinearLayout>
上一篇:mobile开发网址
下一篇:android_turnOverSilent
登录 注册