1
、应用程序启动后,显示界面如图
1
所示。
2、创建一个后台服务,该服务启动后在一个新的线程中持续地以 1 秒间隔更新
通知栏,交替显示两个不同的图标,如图
2
、图
3
所示。(图不整了。。。)
启动服务的方式有两种:一种是通过startservice来启动,不过这样的方式的最为普通的方式,通过这样方式启动的服务不能由用户来控制其随意的开启和暂停,只能通过stopservice来暂定;还有一种方式是通过绑定服务bindservice来启动,这种启动方式用户可以通过bindservice和unbindservice来控制服务的开始与停止。实验4.1用的是第一种启动方式。
界面就两个按钮,代码实现较为简单,代码如下:
<?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:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/startbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动服务"/>
<Button
android:id="@+id/stopbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"/>
</LinearLayout>
</LinearLayout>
主函数也比较简单,重写onCreate方法,创建service(这里用的是异步服务)来启动。代码如下:
package com.example.myapplication7;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button startbtn;
private Intent serviceIntent;
private Button stopbtn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startbtn=findViewById(R.id.startbtn);
stopbtn=findViewById(R.id.stopbtn);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = " myapplication7";
String channelName = "MyApplication7";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
createNotificationChannel(channelId, channelName, importance);
}
final Intent serviceIntent = new Intent(MainActivity.this,MyService.class);
startbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
startService(serviceIntent);
}
});
stopbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
stopService(serviceIntent);
}
});
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}}
除此之外还要创建MyService服务类继承service,并且要在里面重写service的生命周期函数
package com.example.myapplication7;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import androidx.core.app.NotificationCompat;
public class MyService extends Service {
private NotificationCompat.Builder builder=null;
private NotificationManager nm;
public MyService() {
}
public void notifyMe(){
nm=(NotificationManager)getApplicationContext().getSystemService( Context.NOTIFICATION_SERVICE );
builder=new NotificationCompat.Builder(getApplicationContext(),"startbtn");
builder.setContentTitle(getApplicationContext() .getString(R.string.app_name))
.setContentText("系统自动消息接收者")
.setSmallIcon(R.drawable.arrowright)
.setSound(null)
.setAutoCancel(true);
Notification notification=builder.build();
nm.notify(0,notification);
}
@Override
public void onCreate() {
super.onCreate();
workThread = new Thread(backgroundWork);
notifyMe();
}
public int onStartCommand(Intent intent, int falg, int startId) {
if(!workThread.isAlive()){
workThread.start();
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
workThread.interrupt();
nm.cancel(0);
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
private Thread workThread;
private Runnable backgroundWork = new Runnable() {
@Override
public void run() {
boolean flag=true;
try{
while(!Thread.interrupted()){
if(flag){
builder.setSmallIcon(R.drawable.arrowright);
nm.notify(0,builder.build());
flag=false;
}else{
builder.setSmallIcon(R.drawable.leftedd);
nm.notify(0,builder.build());
flag=true;
}
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
注意:在里面添加图标图片的时候,要在网上寻找后缀名为.png或者.jpg的图片文件。
还要在清单文件中声名关于service的相关权限:
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.myapplication7.MY_SERVICE"/>
</intent-filter>
</service>
其实不需要手动添加,当你在新建一个service服务类的时候会显示出选项供你选择,选择完后,系统默认在清单文件中自动添加。