您的位置:首页 > 编程语言

《第一行代码》第二版 学习总结30 前台服务基本使用

2018-03-26 21:08 656 查看
      最近利用下班时间,找了看什么书比较适合初学android的朋友,很多人推荐了这本书,于是就买了一本,感觉看书,思考,动手,再思考和总结这样过程还是很有必要的,于是就打算把自己学习的东西简单的总结一下;方便自己以后查找,也有利于学习的巩固。在这里首先要感谢一下书籍的作者——郭霖前辈。
      服务在Android中是一个很常见的概念,通常都会在后台一直默默的运行,为其他的应用提供功能支持;但是在系统内存不足时,这些服务往往会成为被回收的首要对象,但是某些重要的服务需要在可控的情况下关闭,这个时候可以采用前台服务;使用很简单,下面我们就来具体看一看用法。本文示例代码链接
1,使用步骤
      对于基本概念,前台服务和服务没有定义上本质的区别;只需要在定义的时候重写onCreate()方法的部分代码,主要功能就是展现前台服务的图标以及服务信息;和“通知十分的类似”(想要了解更多关于通知的知识可以点击查看
      使用步骤也很简单,主要是:
第一步:定义服务,重写onCreate()方法
第二步:在onCreate()方法中构建一个描述自定义服务的通知
第三步:通过startForeground()方法使该服务成为一个前台服务,显示在系统状态栏

2,示例代码

MainActivity.java代码:
package com.hfut.operationforegroundservice;

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.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void startMySevice(View view){
Intent intent=new Intent(this,ForegroundService.class);
startService(intent);
}

public void bindMySevice(View view){
Intent intent=new Intent(this,ForegroundService.class);
bindService(intent,new MyServiceConnection(),BIND_AUTO_CREATE);
}

private class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
System.out.println("绑定服务成功了");
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
System.out.println("解绑服务成功了");

}
}
}
ForegroundService.java代码:
package com.hfut.operationforegroundservice;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class ForegroundService extends Service {
public ForegroundService() {
}

@Override
public void onCreate() {
super.onCreate();
Intent intent=new Intent(this,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
Notification notification=new Notification.Builder(this)
.setContentTitle("前台服务")
.setContentText("当前正在执行...")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(pendingIntent)
.build();
startForeground(1,notification);
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//返回一个IBinder的实现类Binder的对象,唯一一个,除非自定义,后续使用大部分都是自定义配合AIDL使用
return new Binder();
}

}
layout_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.hfut.operationforegroundservice.MainActivity">

<Button
android:layout_marginTop="25dp"
android:textSize="15dp"
android:text="开启服务"
android:onClick="startMySevice"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:textSize="15dp"
android:layout_marginTop="15dp"
android:text="绑定服务"
android:onClick="bindMySevice"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

主配置文件AndroidManifest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hfut.operationforegroundservice">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".ForegroundService"
android:enabled="true"
android:exported="true"></service>
</application>

</manifest>
3,运行结果

第一步:运行程序
                               



第二步:点击“开启服务”或者“绑定服务”(两种区别请点击查看)
                               


                               


总结:前台服务使用很简单,重要的是控制和设计它的使用;还有就是这里的通知图标是白色的,这是因为在5.0之后系统不允许通知图标带有RGB颜色涂成;所以只能显示纯色形状;想做有特色的通知图标,可以把背景色设置为透明,用纯白色绘制形状(有的说红色也可以,没有验证过),这样可以展示纯白色的形状。[b]本文示例代码链接[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息