Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีการใช้ start Foreground สำหรับบริการ?


ก่อนเข้าตัวอย่างเราควรรู้ว่าบริการใน Android คืออะไร บริการจะดำเนินการเบื้องหลังโดยไม่โต้ตอบกับ UI และทำงานได้แม้กิจกรรมจะถูกทำลาย

ตัวอย่างนี้สาธิตเกี่ยวกับวิธีการใช้งาน start Foreground สำหรับบริการ

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml

 
ในโค้ดด้านบน เราได้ดูข้อความ เมื่อผู้ใช้คลิกที่มุมมองข้อความ มันจะเริ่มต้น startForeground()

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java

แพ็คเกจ com.example.andy.myapplication;นำเข้า android.app.ActivityManager;นำเข้า android.content.Context;นำเข้า android.content.Intent;นำเข้า android.support.v7.app.AppCompatActivity;นำเข้า android.os.Bundle นำเข้า android.util.Log นำเข้า android.view.View นำเข้า android.widget.TextView คลาสสาธารณะ MainActivity ขยาย AppCompatActivity { @Override ป้องกันโมฆะ onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); ข้อความ TextView สุดท้าย =findViewById (R.id.text); text.setOnClickListener (View.OnClickListener ใหม่ () { @แทนที่โมฆะสาธารณะ onClick (ดู v) { ถ้า (isMyServiceRunning (service.class)) { text.setText ("หยุด"); stopService (เจตนาใหม่ (MainActivity.this บริการ) .class)); } else { text.setText ("Started"); startService (เจตนาใหม่ (MainActivity.this, service.class)); } } }); } บูลีนส่วนตัว isMyServiceRunning (คลาส  serviceClass) { ตัวจัดการกิจกรรม =(ActivityManager) getSystemService (Context.ACTIVITY_SERVICE); สำหรับ (ActivityManager.RunningServiceInfo บริการ:manager.getRunningServices (Integer.MAX_VALUE)) { if (serviceClass.getName().equals(service.service.getClassName())) { คืนค่าเป็น true; } } คืนค่าเท็จ }}

ในรหัสด้านบนเพื่อเริ่มและหยุดบริการ เราใช้เจตนาและผ่านบริบทและคลาสบริการ ตอนนี้สร้างคลาสบริการในโฟลเดอร์แพ็คเกจเป็น service.class และเพิ่มรหัสต่อไปนี้ –

แพ็คเกจ com.example.andy.myapplication;นำเข้า android.annotation.TargetApi;นำเข้า android.app.Notification;นำเข้า android.app.NotificationChannel;นำเข้า android.app.NotificationManager;นำเข้า android.app.PendingIntent;นำเข้า android แอป บริการ นำเข้า android.content.Context นำเข้า android.content.Intent นำเข้า android.graphics.Color นำเข้า android.media.MediaPlayer นำเข้า android.os.Build นำเข้า android.os.IBinder นำเข้า android.support .annotation.RequiresApi;import android.support.v4.app.NotificationCompat;import android.widget.Toast;บริการคลาสสาธารณะขยายบริการ { @Override สาธารณะ IBinder onBind (เจตนาเจตนา) { คืนค่า null; } @แทนที่โมฆะสาธารณะ onCreate() { super.onCreate(); } @TargetApi(Build.VERSION_CODES.O) @RequiresApi(api =Build.VERSION_CODES.JELLY_BEAN) @Override สาธารณะ int onStartCommand(เจตนาเจตนา แฟล็ก int int startId) { Toast.makeText (นี่ "บริการแจ้งเตือนที่เริ่มต้นโดยผู้ใช้ ", ขนมปังปิ้ง.LENGTH_LONG).show(); สตริง NOTIFICATION_CHANNEL_ID ="com.example.andy.myapplication"; String channelName ="บริการพื้นหลังของฉัน"; NotificationChannel chan =ใหม่ NotificationChannel (NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE); chan.setLightColor(Color.BLUE); chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); ผู้จัดการการแจ้งเตือน =(NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE); ยืนยันผู้จัดการ !=null; manager.createNotificationChannel(จัง); เจตนา notificationIntent =เจตนาใหม่ (นี้ MainActivity.class); PendingIntent pendingIntent =PendingIntent.getActivity (นี่ 0, การแจ้งเตือนเจตนา, 0); การแจ้งเตือน =Notification.Builder ใหม่ (this,NOTIFICATION_CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("My Awesome App") .setContentIntent(pendingIntent).build(); startForeground(1337, การแจ้งเตือน); ส่งคืน START_STICKY; } @RequiresApi(api =Build.VERSION_CODES.N) @Override public void onDestroy() { super.onDestroy(); หยุดเบื้องหน้า (จริง); Toast.makeText(นี่คือ "บริการแจ้งเตือนถูกทำลายโดยผู้ใช้", Toast.LENGTH_LONG).show(); }}

ในโค้ดด้านบนนี้ เราได้ใช้ช่องการแจ้งเตือนและตัวสร้างการแจ้งเตือนดังที่แสดงด้านล่าง –

String NOTIFICATION_CHANNEL_ID ="com.example.andy.myapplication";String channelName ="My Background Service";NotificationChannel chan =new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);chan.setLightColor(Color.BLUE);chan.setLightColor(Color.BLUE);chan .setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);NotificationManager manager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);assert manager !=null;manager.createNotificationChannel(chan);Intent notificationIntent =new Intent (this, MainActivity.class pendingIntent) PendingIntent.getActivity(this, 0, alertIntent, 0);Notification notification =new Notification.Builder(this,NOTIFICATION_CHANNEL_ID).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("My Awesome App")).setContentIntent(pendingIntent).build ();startForeground(1337, การแจ้งเตือน);

ในการเริ่มและหยุดพื้นหน้าใช้รหัสต่อไปนี้ –

startForeground(1337, การแจ้งเตือน);stopForeground(จริง);

ขั้นตอนที่ 4 − เพิ่มรหัสต่อไปนี้ใน manifest.xml

     <หมวดหมู่ android:name ="android.intent.category.LAUNCHER" />    

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก จากนั้นตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ –

วิธีการใช้ start Foreground สำหรับบริการ?

ในผลลัพธ์ข้างต้นเป็นหน้าจอเริ่มต้น คลิกที่ Text view จะเป็นการเริ่มบริการแจ้งเตือนดังที่แสดงด้านล่าง –
วิธีการใช้ start Foreground สำหรับบริการ? จากผลลัพธ์ข้างต้น บริการเริ่มทำงานแล้ว คลิกที่มุมมองข้อความ จะหยุดบริการแจ้งเตือนดังที่แสดงด้านล่าง -

วิธีการใช้ start Foreground สำหรับบริการ?