【android的Service的实例】眼前多少难甘事,自古男儿当自强。这篇文章主要讲述android的Service的实例相关的知识,希望能为你提供帮助。
package com.android.service;
import android.app.IntentService;
import android.content.Intent;
public class HelloIntentService extends IntentService{
public HelloIntentService() {
super("HelloIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
System.out.println("休息8秒");
try{
Thread.sleep(8000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
public void onDestory(){
System.out.println("执行onHandleIntent之后会自动调用!");
super.onDestroy();
}
}
package com.android.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class HelloService extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
super.onCreate();
}
public void onDestory(){
super.onDestroy();
}
public void onStart(Intent intent,int startId) {
// TODO Auto-generated method stub
System.out.println("启动Service,休眠10秒");
try{
Thread.sleep(8000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
package com.android.service;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(
MainActivity.this,HelloIntentService.class);
startService(intent);
}
});
Button button2=(Button)findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(
MainActivity.this,HelloIntentService.class);
stopService(intent);
}
});
Button button3=(Button)findViewById(R.id.button3);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(
MainActivity.this,HelloService.class);
startService(intent);
}
});
}
@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;
}
}
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<
Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动IntentService"
/>
<
Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止IntentService"
/>
<
Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动Service"
/>
<
/LinearLayout>
推荐阅读
- Android 手机卫士--平移动画实现
- css怎么让Android 浏览器焦点在文本输入框时不自动放大页面
- TypeScript编译环境和tsconfig.json文件 – TypeScript开发教程
- 如何实现最近最少使用(LRU)页面替换算法程序()
- Python中的numpy.subtract()用法详细介绍
- AngularJS指令完整参考指南
- C++如何使用std::search(代码示例)
- Python Numpy介绍和用法经典指南
- JavaScript中的函数介绍和用法详细指南