編輯:關於Android編程
我們常常在開發的時候,通過獲取系統已啟動的服務來判斷該服務器是否還需要再啟動。
而本文將介紹android設備中已啟動的服務,並判斷某一服務是啟動
1.根據ACTIVITY_SERVICE獲取系統服務
activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
2.獲取系統中正在運行的服務
runningList = activityManager.getRunningServices(30);//代表我們希望返回的服務數目大小 30
效果圖:
實例代碼如下<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+MSkgYWN0aXZpdHlfbWFpbi54bWw8L3A+CjxwPjwvcD4KPHByZSBjbGFzcz0="brush:java;">
3)MainActivity.javaAndroidServiceDemo 顯示所有服務: Settings 獲取所有服務
package com.example.androidservicedemo; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private TextView displayAllServiceTv; private Button getAllServiceBtn; private ActivityManager activityManager; private List runningList; private String myServiceClassName = "org.linphone.LinphoneService"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final String defaultStr = this.getResources().getString(R.string.display_all_service); displayAllServiceTv = (TextView) this.findViewById(R.id.display_service_tv); getAllServiceBtn = (Button) this.findViewById(R.id.service_btn); getAllServiceBtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub StringBuffer displaySb = new StringBuffer(); displaySb.append(defaultStr).append("\n"); activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); runningList = activityManager.getRunningServices(30);//代表我們希望返回的服務數目大小 30 displaySb.append(myServiceClassName).append(":").append(myServiceIsStart(runningList,myServiceClassName)).append("\n"); displayAllServiceTv.setText(getAllService(runningList,displaySb).toString()); } }); } private boolean myServiceIsStart(List runningList,String myServiceClassName){ boolean isStart = false; for(ActivityManager.RunningServiceInfo runningService:runningList){ if(myServiceClassName.equals(runningService.service.getClassName())){ isStart = true; break; } } return isStart; } private StringBuffer getAllService(List runningList,StringBuffer displaySb){ for(ActivityManager.RunningServiceInfo runningService:runningList){ displaySb.append(runningService.service.getClassName()).append("\n"); } return displaySb; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
最近看到新浪微博頂部欄的微博分組效果很炫,從網上查了一些資料明白原來是用PopupWindow實現的,今天自己也寫了一個例子實現了這種效果,希望對大家有幫助。PopupW
運行時權限介紹Android 6.0在我們原有的AndroidManifest.xml聲明權限的基礎上,又新增了運行時權限動態檢測,以下權限都需要在運行時判斷: 
今天主要修改了幾個bug,然後改了下背景圖片和圖標,添加了變動人數的功能,即:指定參與AA的人數,參與AA計算。還有就是利用getResources().getIdent
首先看看本節的流程: 計算圓的頂點坐標: 我們先要明白OpenglES中圓是怎麼畫的,前面我們已經知道三角形扇的繪制方式,我們的圓其實也可