Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1155414
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: 嵌入式

2010-08-04 08:49:18

ActivityMain.java
 
 

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>


阅读(681) | 评论(0) | 转发(0) |
0

上一篇:mobile开发网址

下一篇:android_turnOverSilent

给主人留下些什么吧!~~