編輯:關於Android編程
在 \res\xml下新建example_appwidget_info.xml
在layout下新建process_widget.xml,用來顯示widget的布局
新建一個類ProcessWidget繼承AppWidgetProvider
import cn.itcast.mobilesafe.service.UpdateWidgetService; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.sax.StartElementListener; public class ProcessWidget extends AppWidgetProvider { Intent intent; @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); intent = new Intent(context,UpdateWidgetService.class); context.stopService(intent); } @Override public void onEnabled(Context context) { super.onEnabled(context); intent = new Intent(context,UpdateWidgetService.class); context.startService(intent); } }
import android.app.PendingIntent; import android.app.Service; import android.appwidget.AppWidgetManager; import android.content.ComponentName; import android.content.Intent; import android.graphics.Color; import android.os.IBinder; import android.widget.RemoteViews; public class UpdateWidgetService extends Service { private Timer timer ; private TimerTask task; private AppWidgetManager widgetmanager; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { timer = new Timer(); widgetmanager = AppWidgetManager.getInstance(getApplicationContext()); task = new TimerTask() { @Override public void run() { // 更新widget的界面 ComponentName name = new ComponentName("cn.itcast.mobilesafe", "cn.itcast.mobilesafe.receiver.ProcessWidget"); RemoteViews views = new RemoteViews("cn.itcast.mobilesafe", R.layout.process_widget); views.setTextViewText(R.id.process_count, "進程數目"+TaskUtil.getProcessCount(getApplicationContext())); views.setTextColor(R.id.process_count, Color.RED); views.setTextViewText(R.id.process_memory, "可用內存"+TaskUtil.getMemeorySize(getApplicationContext())); views.setTextColor(R.id.process_memory, Color.RED); Intent intent = new Intent(UpdateWidgetService.this,LockScreenReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0); views.setOnClickPendingIntent(R.id.btn_clear, pendingIntent); widgetmanager.updateAppWidget(name, views); } }; timer.scheduleAtFixedRate(task, 1000, 2000); super.onCreate(); } @Override public void onDestroy() { timer.cancel(); timer = null; task =null; super.onDestroy(); } }TaskUtil的代碼如下:
import java.util.List; import android.app.ActivityManager; import android.app.ActivityManager.MemoryInfo; import android.app.ActivityManager.RunningAppProcessInfo; import android.content.Context; import android.text.TextUtils; public class TaskUtil { public static void killAllProcess(Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); Listrunningapps = am.getRunningAppProcesses(); for (RunningAppProcessInfo info : runningapps) { String packname = info.processName; am.killBackgroundProcesses(packname); } } public static int getProcessCount(Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List runningapps = am.getRunningAppProcesses(); return runningapps.size(); } public static String getMemeorySize(Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo outInfo = new ActivityManager.MemoryInfo(); am.getMemoryInfo(outInfo); return TextFormater.getDataSize( outInfo.availMem); } }
通過兩張圖對比,,不難發現布局異常!看代碼 android:layout_hei
下面先來一張效果圖 根據圖片分析,要實現的有側邊欄DrawerLayout,ActionBar的顏色和菜單以及ActionBarDrawerTogg
1 秒殺業務分析正常電子商務流程(1)查詢商品;(2)創建訂單;(3)扣減庫存;(4)更新訂單;(5)付款;(6)賣家發貨秒殺業務的特性(1)低廉價格;(2)大幅推廣;(
一、說起進度條,必須說說條形進度條,經常都會使用到嘛,特別是下載文件進度等等,還有像騰訊QQ安裝進度條一樣,有個進度總給人良好的用戶體驗。先來找圖看看,做這