編輯:初級開發
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.RemoteVIEws;
public class BatteryMonitorWidget extends AppWidgetProvider
{
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
{
context.startService(new Intent(context, BatteryMonitorReceiver.class));
}
public static class BatteryMonitorReceiver extends Service
{
private int currentBatteryLevel = 0;
private BroadcastReceiver batteryReceiver = new BroadcastReceiver()
{
@Override
public void onReceive( Context context, Intent intent )
{
currentBatteryLevel = intent.getIntExtra( "level", 0 );
}
};
@Override
public void onStart( Intent intent, int startId )
{
registerReceiver( batteryReceiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED ) ); //注冊一個動作改變事件捕獲,這裡為電量改變時即ACTION_BATTERY_CHANGED
RemoteViews views = new RemoteVIEws( getPackageName(), R.layout.main ); //從layout下main.XML中構造widgets
//這裡我們應該判斷currentBatteryLevel的數值來顯示圖片電量等級,比如battery_level_1或battery_level_5等等,下面是n
views.setImageViewResource( R.id.ImageVIEw01, R.drawable.battery_level_n );
views.setTextVIEwText( R.id.myText, currentBatteryLevel + "%" ); //從BroadcastReceiver讀取當前的電量等級
ComponentName thisWidget = new ComponentName( this, BatteryMonitorWidget.class );
AppWidgetManager manager = AppWidgetManager.getInstance( this );
manager.updateAppWidget( thisWidget, updateVIEws ); //開始執行了
}
@Override
public IBinder onBind( Intent intent )
{
return null;
}
}
}
三、範例程式:Activity與Service間之溝通先執行ac01:這個ac01立即啟動myService,定時連續傳來數字,如下:數字連續增加下去。其程式碼為:/*
很多初入Android的開發者可能會發現經常遇到Force Close或ANR這樣的問題,一般我們通過android系統的錯誤日志打印工具Logcat可以看到出錯的內容
android 有一種叫消息隊列的說法,這裡我們可以這樣理解:假如一個隧道就是一個消息隊列,那麼裡面的每一部汽車就是一個一個消息,這裡我們先忽略掉超車等種種因素,只那麼
第一部分:arsc文件漢化目前市面上最流行的漢化方式,就是漢化這部分主文件。我們開始吧:首先,打開androidResEdit1.3然後把resources.arsc文