編輯:關於Android編程
一,首先做一個監聽器:
public class StartBroadcastReceiver extends BroadcastReceiver{
private static final String ACTION = "android.intent.action.BOOT_COMPLETED";
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)){
Intent i= new Intent(Intent.ACTION_RUN);
i.setClass(context, TService.class);
context.startService(i);
}
}
}
二,然後再做一個service:
package com.testService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class TService extends Service {
/**
* 創建Handler對象,作為進程傳遞postDelayed之用
*/
private Handler objHandler = new Handler();
private int intCounter = 0;
private static final String TAG = "TService";
private NotificationManager notificationManager;
private Runnable mTasks = new Runnable() {
public void run() {
intCounter++;
Log.i("HIPPO", "Counter:" + Integer.toString(intCounter));
objHandler.postDelayed(mTasks, 1000);
}
};
public void onCreate() {
Log.d(TAG, "============> TService.onCreate");
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
super.onCreate();
}
public void onStart(Intent intent, int startId) {
Log.i(TAG, "============> TService.onStart");
objHandler.postDelayed(mTasks, 1000);
super.onStart(intent, startId);
}
public IBinder onBind(Intent intent) {
Log.i(TAG, "============> TService.onBind");
return null;
}
public class LocalBinder extends Binder {
public TService getService() {
return TService.this;
}
}
public boolean onUnbind(Intent intent) {
Log.i(TAG, "============> TService.onUnbind");
return false;
}
public void onRebind(Intent intent) {
Log.i(TAG, "============> TService.onRebind");
} www.2cto.com
public void onDestroy() {
Log.i(TAG, "============> TService.onDestroy");
notificationManager.cancel(R.string.service_start);
objHandler.removeCallbacks(mTasks);
super.onDestroy();
}
private void showNotification() {
Notification notification = new Notification(R.drawable.icon,
"SERVICE START", System.currentTimeMillis());
Intent intent = new Intent(this, testService.class);
intent.putExtra("FLG", 1);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
notification.setLatestEventInfo(this, "SERVICE", "SERVICE START",
contentIntent);
notificationManager.notify(R.string.service_start, notification);
}
}
三,再做一個主程序:
package com.testService;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class testService extends Activity {
private Button button01;
private Button button02;
private int flg;
private Intent tsIntent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button01 = (Button)findViewById(R.id.Button01);
button02 = (Button)findViewById(R.id.Button02);
tsIntent = this.getIntent();
Bundle bundle = tsIntent.getExtras();
if (bundle == null){
flg = 1;
DisplayToast(this,"Service Start",2);
startService();
finish();
}else{
DisplayToast(this,"Service Stop",2);
stopService();
finish();
}
button01.setOnClickListener(Listener01);
button02.setOnClickListener(Listener02);
}
Button.OnClickListener Listener01 = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService();
}
};
Button.OnClickListener Listener02 = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService();
}
};
private void startService() {
Intent i = new Intent(testService.this, TService.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startService(i);
}
private void stopService() {
Intent i = new Intent(testService.this, TService.class);
this.stopService(i);
}
public static void DisplayToast(Context context , String str , int time){
if (time == 1){
//短時間顯示Toast
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}else if (time == 2){
//長時間顯示Toast
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
}
}
}
四,最後在androidmenfest.xml中注冊一下接受廣播器和服務以及開通權限:
接收廣播器:
<receiver android:name=".StartBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
服務:
<service android:name=".TService"
android:label="TService"
android:icon="@drawable/icon"
android:enabled="true"
android:exported="true"
android:process=":remote">
</service>
復制代碼
注冊權限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
注意:接受廣播和服務都必須注冊在:<application>這裡注冊</application>
Android開源框架Universal-Image-Loader就像圖片加載守護者,為我們提供了豐富的功能特性:(1)多線程加載圖像(異步或同步);(2)高度可定制化i
Android Studio安裝後發現所有的中文,不管是界面上的還是輸出的log中的中文都變成小框框 可以肯定是字體的問題 解決:菜單File->set
最近一直在學習自定義View相關的知識,今天給大家帶來的是QQ健康界面的實現。先看效果圖: 可以設置數字顏色,字體顏色,運動步數,運動排名,運動平均步數,虛線下方的藍色指
在網上看了比較多的關於Tab的教程,發現都很雜亂。比較多的用法是用TitlePagerTabStrip和ViewPaper。不過TitlePagerTabStrip有個很