移动应用开发技术-实验4.1

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服务类的时候会显示出选项供你选择,选择完后,系统默认在清单文件中自动添加。

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
UCGUI Builder 4.1是一个图形用户界面设计工具,用于开发嵌入式系统。通过UCGUI Builder 4.1,开发人员可以方便地创建和设计嵌入式系统的用户界面,使其更加易于使用和视觉上吸引人。 UCGUI Builder 4.1具有以下特点和功能: 1. 可视化界面设计:UCGUI Builder 4.1提供了一个直观的界面,开发人员可以方便地拖放和调整各种UI元素,如按钮、文本框、菜单等,以创建所需的用户界面。 2. 多样化的UI元素:UCGUI Builder 4.1提供了丰富的UI元素库,使开发人员能够选择各种按钮样式、文本框样式、列表样式等,以满足不同的用户界面需求。 3. 可定制性:UCGUI Builder 4.1允许开发人员自定义UI元素的外观和行为,以满足特定应用的需求。开发人员可以调整按钮的颜色、字体和大小,定义菜单的布局和功能等。 4. 代码生成:UCGUI Builder 4.1可以自动生成所设计界面的代码,使开发人员无需手动编写UI相关的代码。生成的代码可以直接在嵌入式系统中使用,并与其他系统模块进行集成。 5. 资源管理:UCGUI Builder 4.1提供了资源管理功能,开发人员可以方便地导入和管理所需的图片、字体等资源文件,以便在设计界面时使用。 综上所述,UCGUI Builder 4.1是一个强大的图形用户界面设计工具,为开发嵌入式系统的人员提供了简便、高效的界面开发方式。它的可视化界面设计、丰富的UI元素库和代码生成功能,使得开发人员能够快速设计和实现各种吸引人的用户界面,并提供良好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值