举例说明如何在android中使用Service
来源:互联网 发布:淘宝生意经 编辑:程序博客网 时间:2024/05/22 09:01
一旦服务在onStart()被启动后,程序就写完了,只要继承Service对象里,编写如BrocastReceiver这类对象,就可以开始聆听操作系统的广播的信息了。
如果直接编写Service的话,无法证明服务真的在后台运行,所示在程序中开户一个Runnable进程,每一秒都在控制台(console)里输出的微尘数(整数counter),用来证明系统服务在运行中。
程序如下所示:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class A04Activity extends Activity {
private Button b01,b02;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b01=(Button)findViewById(R.id.button01);
b02=(Button)findViewById(R.id.button02);
b01.setBackgroundColor(Color.GREEN);
b02.setBackgroundColor(Color.RED);
b01.setText("启动Service");
b02.setText("停止Service");
b01.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/**
*虽然打开Service的方法与打开Activity都用Intent对象,但是它们的方法有明显的不同之处:
*1.打开服务用startService(Intent intent),而打开Activity用startActivity(Intent intent)
*2.结束服务用stopService(Intent intent),而结束Activity用finish();
*/
Intent i=new Intent(A04Activity.this,ServiceTest.class);
//设置新Task的方式
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(i);
}
});
b02.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(A04Activity.this,ServiceTest.class);
stopService(i);
}
});
}
}
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class ServiceTest extends Service{
//创建Handler对象,作为进程传递postDelayed之用
Handler h=new Handler();
int counter;
//创建线程
Runnable r=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
counter++;
//调用Log对象,在LogCat中显示信息,以验证Service正在运行
Log.i("I LOVE YOU", "count:"+Integer.toString(counter));
System.out.println("--------------------------");//注意用此语句输出与Log.i(tag,String s);的不同之处
//每一秒调用线程,反复运行
h.postDelayed(r, 1000);
}
};
public void onStart(Intent intent,int startId){
//服务开始,每一秒调用线程
h.postDelayed(r, 3000);
super.onStart(intent, startId);
}
public void onCreate(){
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onDestory(){
//若服务结束,则删除线程r
h.removeCallbacks(r);
super.onDestroy();
}
}
注意要在AndroidManifest.xml中配置Service如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.a04"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".A04Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 创建Service类,并指定类的名称 -->
<!-- 指定exported属性为true.说明其他程序可以访问该类 -->
<service
android:name=".ServiceTest"
android:exported="true"
android:process=":remote"
></service>
</application>
</manifest>
- 举例说明如何在android中使用Service
- 举例说明如何在android中利用Service来实现后台发送邮件功能
- 举例说明如何在android中实现设置黑名单的功能--AudioManager、PhoneStateListener的使用
- 举例说明在android中使用BroadcastReceiver之二-------查看电池电量
- 举例说明android中BroadcastReceiver的使用
- 举例说明Android中AnalogClock的使用
- 举例说明如何在android模拟器中创建模拟SD存储卡
- 举例说明如何在C#中使用C/C++写的DLL
- 举例说明如何在C#中使用C/C++写的DLL
- Android如何在service中显示Dialog
- [Android] 在Service中使用Toast
- android 在service中使用toast
- Android:在Service中使用Toast
- 举例说明在android中使用正则表达式去掉字符串中标点符号及其他一些特殊符号的方法
- android 如何使用Service
- android 在service中如何向activity传递数据
- 在Android中如何侦听Service内部的状态
- 如何在Android中利用AIDL添加service
- 0.ring3-NtMapViewOfSection注入
- django模型字符串字段写mysql字符串的处理
- 精通Visual C++图像编程之位图的特技显示(学习笔记)
- 学生管理系统 重要语句的学习
- Android自定义Dialog显示GridView
- 举例说明如何在android中使用Service
- netlink socket
- 判断CPU是否64位,以及linux操作系统是否64位
- C# FTP 文件 文件夹操作 上传 下载
- Java异常处理及异常机制介绍
- JSP --Jsp:param
- paip.手机ROOT过程总结
- C语言内存管理详解
- 犯我强汉天威者,穷搜天下,万里追杀,覆其巢,断其苗裔,戮其身,追其魂,屠其魄,虽远必诛!!!