安卓四大组件-服务-2_服务基本用法
来源:互联网 发布:python编写抢购软件 编辑:程序博客网 时间:2024/06/05 14:59
服务基本用法
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/start_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Service"/>
<Button
android:id="@+id/stop_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Service"/>
<Button
android:id="@+id/bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bind Service"/>
<Button
android:id="@+id/unbind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Unbind Service"/>
<Button
android:id="@+id/start_intent_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start IntentService"/>
</LinearLayout>
packagecom.example.servicetest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extendsAppCompatActivity implementsView.OnClickListener{
privateMyService.DownloadBinder downloadBinder;
privateServiceConnection connection= newServiceConnection() {
@Override
public voidonServiceDisconnected(ComponentName name) {
}
@Override
public voidonServiceConnected(ComponentName name, IBinder service) {
downloadBinder = (MyService.DownloadBinder) service;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
};
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startService = (Button) findViewById(R.id.start_service);
Button stopService = (Button) findViewById(R.id.stop_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
Button bindService = (Button) findViewById(R.id.bind_service);
Button unbindService = (Button) findViewById(R.id.unbind_service);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
Button startIntentService = (Button) findViewById(R.id.start_intent_service);
startIntentService.setOnClickListener(this);
}
@Override
public voidonClick(View v) {
switch(v.getId()) {
caseR.id.start_service:
Intent startIntent = newIntent(this, MyService.class);
startService(startIntent); // 启动服务
break;
caseR.id.stop_service:
Intent stopIntent = newIntent(this, MyService.class);
stopService(stopIntent); // 停止服务
break;
caseR.id.bind_service:
Intent bindIntent = newIntent(this, MyService.class);
bindService(bindIntent, connection,BIND_AUTO_CREATE);// 绑定服务
break;
caseR.id.unbind_service:
unbindService(connection);// 解绑服务
break;
caseR.id.start_intent_service:
// 打印主线程的id
Log.d("MainActivity","Thread id is " + Thread.currentThread(). getId());
Intent intentService = new Intent(this, MyIntentService.class);
startService(intentService);
break;
default:
break;
}
}
}
packagecom.example.servicetest;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class MyIntentService extendsIntentService {
publicMyIntentService() {
super("MyIntentService");// 调用父类的有参构造函数
}
@Override
protected voidonHandleIntent(Intent intent) {
// 打印当前线程的id
Log.d("MyIntentService","Thread id is " + Thread.currentThread(). getId());
}
@Override
public voidonDestroy() {
super.onDestroy();
Log.d("MyIntentService","onDestroy executed");
}
}
packagecom.example.servicetest;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
/**
* 活动和服务进行通信
*/
public classMyService extendsService {
publicMyService() {
}
privateDownloadBinder mBinder= newDownloadBinder();
classDownloadBinder extendsBinder {
public voidstartDownload() {
Log.d("MyService","startDownload executed");
}
public intgetProgress() {
Log.d("MyService","getProgress executed");
return0;
}
}
@Override
publicIBinder onBind(Intent intent) {
returnmBinder;
}
@Override
public voidonCreate() {
super.onCreate();
Log.d("MyService","onCreate executed");
Intent intent = newIntent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0, intent,0);
Notification notification = newNotificationCompat.Builder(this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pi)
.build();
startForeground(1, notification);
}
@Override
public intonStartCommand(Intent intent, int flags,int startId) {
Log.d("MyService","onStartCommand executed");
return super.onStartCommand(intent, flags, startId);
}
@Override
public voidonDestroy() {
super.onDestroy();
Log.d("MyService","onDestroy executed");
}
}
- 安卓四大组件-服务-2_服务基本用法
- 安卓四大组件-服务-1_多线程编程
- 安卓四大组件之service服务
- 安卓四大组件之服务
- 安卓四大组件-Service服务
- 安卓四大组件——Service(服务)
- 安卓四大基本组件
- 安卓入门--四大组件--Service基本用法
- Android四大组件--服务
- 安卓四大组件以及用法
- 【Android】安卓学习笔记之服务的基本用法
- Android四大组件之服务Service的用法
- Android四大基本组件(2)之Service 服务与Content Provider内容提供者
- Android四大组件之服务
- Android四大组件之服务
- Android四大组件之服务
- Android四大组件之服务
- Android四大组件服务(Service)
- weblogic相关
- 【Codeforces 798 C】+ gcd
- poj1459Power Network_最大流的增广路算法_多源点多汇点问题(Edmord_Karp)
- VirtualBox centos7扩容
- Android:SwipeRefreshLayout和ViewPager滑动冲突的原因和正确的解决方式
- 安卓四大组件-服务-2_服务基本用法
- 百度地图JS使所有标注都在视野范围内
- Android标准调用播放器流程
- 手机号运营商判断php代码
- linux c中access函数
- 【IMWeb训练营作业02 select组件】
- 单调有界收敛准则
- win10安装TensorFlow填坑笔记
- Shiro与Redis集成,集群下的session共享