安卓-启动前台服务
来源:互联网 发布:广州打车软件 编辑:程序博客网 时间:2024/05/29 23:46
为什么要设置前台服务?
但是服务的系统 优先级还是比较低的,当系统出现内存不足的情况时,就有可能会回收掉正在后台运行的服 务。如果你希望服务可以一直保持运行状态,而不会由于系统内存不足的原因导致被回收, 就可以考虑使用前台服务。前台服务和普通服务最大的区别就在于,它会一直有一个正在运 行的图标在系统的状态栏显示,下拉状态栏后可以看到更加详细的信息,非常类似于通知的 效果。当然有时候你也可能不仅仅是为了防止服务被回收掉才使用前台服务的,有些项目由于特殊的需求会要求必须使用前台服务
设置前台服务的关键API:startForeground
启动前台服务可以通过startService或bindService
下面分别演示下通过以上两种方式启动前台服务的效果。
启动前台服务后。状态栏中会显示一个小图标,下拉通知栏可以看到和提示消息一样的通知栏详情,点击可以跳转到相应的界面。
测试界面图
服务中打印了日志,大家可以参考:
通过startService时打印日志如下:
02-18 16:14:47.086 19659-19659/com.mobile.cdtx.blog I/ForegroundService: onCreate:
02-18 16:14:47.106 19659-19659/com.mobile.cdtx.blog I/ForegroundService: onStartCommand:
通过stopService时打印日志如下:
02-18 16:15:12.506 19659-19659/com.mobile.cdtx.blog I/ForegroundService: onDestroy:
通过bindService时打印日志如下:
02-18 16:15:20.776 19659-19659/com.mobile.cdtx.blog I/ForegroundService: onCreate:
02-18 16:15:20.816 19659-19659/com.mobile.cdtx.blog I/ForegroundService: onBind:
02-18 16:15:20.816 19659-19659/com.mobile.cdtx.blog I/ForegroundService: doSomeThingOne:
02-18 16:15:20.816 19659-19659/com.mobile.cdtx.blog I/ForegroundService: doSomeThingTwo:
02-18 16:15:20.816 19659-19659/com.mobile.cdtx.blog I/ForegroundService: sayEnglish:
02-18 16:15:20.816 19659-19659/com.mobile.cdtx.blog I/ForegroundService: songEnglish:
通过unbindService时打印日志如下:
02-18 16:15:35.716 19659-19659/com.mobile.cdtx.blog I/ForegroundService: onUnbind:
02-18 16:15:35.716 19659-19659/com.mobile.cdtx.blog I/ForegroundService: onDestroy:
测试代码如下:
主布局文件activity_foreground.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/activity_foreground" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/id_btn_start" android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动前台服务startService"/> <Button android:id="@+id/id_btn_stop" android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="结束前台服务stopService"/> <Button android:id="@+id/id_btn_bind" android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="绑定前台服务bindService"/> <Button android:id="@+id/id_btn_unbind" android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="解绑前台服务unbindService"/></LinearLayout>
主界面代码如下:
import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import com.mobile.cdtx.blog.R;import com.mobile.cdtx.blog.main.service.ForegroundService;public class ForegroundActivity extends AppCompatActivity implements View.OnClickListener{ Button btnStart,btnStop,btnBind,btnUnbind; ForegroundService foregroundService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_foreground); initView();//控件的初始化 } //控件的初始化 private void initView(){ btnStart = (Button)findViewById(R.id.id_btn_start); btnStart.setOnClickListener(this); btnStop = (Button)findViewById(R.id.id_btn_stop); btnStop.setOnClickListener(this); btnBind = (Button)findViewById(R.id.id_btn_bind); btnBind.setOnClickListener(this); btnUnbind = (Button)findViewById(R.id.id_btn_unbind); btnUnbind.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.id_btn_start: startForeground(); break; case R.id.id_btn_stop: stopForeground(); break; case R.id.id_btn_bind: bindForeground(); break; case R.id.id_btn_unbind: unbindForeground(); break; } } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { ForegroundService.LocalBinder binder = (ForegroundService.LocalBinder) service; foregroundService = binder.getService(); //调用服务中的方法 foregroundService.doSomeThingOne(); foregroundService.doSomeThingTwo(); foregroundService.sayEnglish(); foregroundService.songEnglish(); } @Override public void onServiceDisconnected(ComponentName arg0) { } }; //启动前台服务 private void startForeground(){ Intent intent = new Intent(ForegroundActivity.this,ForegroundService.class); startService(intent); } //结束前台服务 private void stopForeground(){ Intent intent = new Intent(ForegroundActivity.this,ForegroundService.class); stopService(intent); } //绑定前台服务 private void bindForeground(){ Intent intent = new Intent(this, ForegroundService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } //解绑前台服务 private void unbindForeground(){ unbindService(mConnection); }}
服务代码如下:
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.v7.app.NotificationCompat;import android.util.Log;import com.mobile.cdtx.blog.R;import com.mobile.cdtx.blog.main.activity.JumpActivity;public class ForegroundService extends Service { private static final String TAG = "ForegroundService"; public ForegroundService() { } private LocalBinder mBinder = new LocalBinder(); public class LocalBinder extends Binder { public ForegroundService getService(){ return ForegroundService.this; } } @Override public IBinder onBind(Intent intent) { Log.i(TAG, "onBind: "); return mBinder; } @Override public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate:"); Intent intent = new Intent(this, JumpActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new NotificationCompat.Builder(this) .setContentTitle("标题") .setContentText("内容") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentIntent(pi) .build(); startForeground(1, notification); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand:"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.i(TAG, "onDestroy:"); } @Override public void onRebind(Intent intent) { Log.i(TAG, "onRebind: "); super.onRebind(intent); } @Override public boolean onUnbind(Intent intent) { Log.i(TAG, "onUnbind: "); return super.onUnbind(intent); } //做事情one public void doSomeThingOne(){ Log.i(TAG, "doSomeThingOne: "); } //做事情two public void doSomeThingTwo(){ Log.i(TAG, "doSomeThingTwo: "); } //说英文 public void sayEnglish() { Log.i(TAG, "sayEnglish:"); } //唱英文歌 public void songEnglish() { Log.i(TAG, "songEnglish:"); }}跳转界面的布局文件如下:
activity_jump.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_jump" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.mobile.cdtx.blog.main.activity.JumpActivity"> <TextView android:gravity="center" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="模拟跳转后的界面"/></RelativeLayout>
跳转界面代码如下:
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import com.mobile.cdtx.blog.R;public class JumpActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jump); }}
最后不要忘记activity和服务均要在AndroidManifest.xml文件中注册。
- 安卓-启动前台服务
- 安卓服务service全解,生命周期,前台服务、后台服务,启动注销、绑定解绑,注册
- 安卓前台服务和后台服务的区别
- 安卓7.0 系统服务启动流程
- 安卓开发:服务的更多技巧--1.使用前台服务
- 安卓-启动服务并让服务自己结束
- 安卓开发40:使用startForeground让android服务前台运行
- 安卓应用每次从后台切换到前台都会启动欢迎界面的问题
- python 作为windows服务启动以及windows服务前台显示
- adb startservice 启动服务出现异常,安卓4.X
- 安卓开发-如何启动service服务总结+实际应用
- 安卓开发-开机时启动电话窃听service服务
- 安卓服务的启动方式之一 bindService unbindService
- 前台服务
- 前台服务
- 前台服务
- 安卓2048的前台界面
- 安卓Service绑定方式启动服务并调用服务中的方法
- 153. Find Minimum in Rotated Sorted Array
- Android View绘制流程
- STL 栈stack的使用
- hdoj2602 Bone Collector
- BLE协议栈构成及各部分概念解析
- 安卓-启动前台服务
- Parallel 线程
- 第27天(就业班) 泛型、泛型的反射、注解
- CGI 与 fast_cgi
- SVM支持向量机原理(三)线性不可分支持向量机与核函数
- HDU4424:Conquer a New Region(并查集 + 贪心)
- Android Studio Plugins
- android倒计时控件
- JIT与JVM的三种执行模式:解释模式、编译模式、混合模式