Computer >> Máy Tính >  >> Lập trình >> Android

Làm thế nào để cập nhật tiếp tục ui từ Dịch vụ Intent trong Android?

Trước khi đi vào ví dụ, chúng ta nên biết dịch vụ Intent trong android là gì. Dịch vụ Intent sẽ thực hiện hoạt động trở lại không đồng bộ. Khi người dùng gọi startService () từ hoạt động, nó không tạo phiên bản cho từng yêu cầu và nó sẽ dừng dịch vụ sau khi thực hiện một số hành động trong lớp dịch vụ, nếu không, chúng tôi cần dừng dịch vụ theo cách thủ công bằng cách sử dụng stopSelf ().

Ví dụ này minh họa về Cách tiếp tục cập nhật ui từ Dịch vụ Intent.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

    
 Trong đoạn mã trên, chúng tôi đã sử dụng chế độ xem văn bản, khi người dùng nhận được dữ liệu từ dịch vụ ý định, nó sẽ cập nhật. 

Bước 3 - Thêm mã sau vào src / MainActivity.java

 package com.example.andy.myapplication; nhập android.content.BroadcastReceiver; nhập android.content.Context; nhập android.content.Intent; nhập android.content.IntentFilter; nhập android.os.Bundle; nhập android. os.Handler; nhập android.support.v4.content.LocalBroadcastManager; nhập android.support.v7.app.AppCompatActivity; nhập android.view.View; nhập android.widget.TextView; nhập java.util.Timer; nhập java. use.TimerTask; public class MainActivity mở rộng văn bản AppCompatActivity {TextView; BroadcastReceiver broadcastReceiver =new BroadcastReceiver () {@Override public void onReceive (Bối cảnh ngữ cảnh, Ý định dự định) {String someValue =Purpose.getStringExtra ("someName"); text.setText (someValue); }}; @Override được bảo vệ void onStart () {super.onStart (); IntentFilter aimFilter =new IntentFilter (); IntentFilter.addAction ("com.example.andy.myapplication"); LocalBroadcastManager.getInstance (this) .registerReceiver (broadcastReceiver, Ý địnhFilter); } @Override được bảo vệ void onCreate (Gói đã lưuInstanceState) {super.onCreate (saveInstanceState); setContentView (R.layout.activity_main); text =findViewById (R.id.text); trình xử lý final Handler =new Handler (); TimerTask timertask =new TimerTask () {@Override public void run () {handler.post (new Runnable () {public void run () {startService (new Intent (MainActivity.this, service.class));}}); }}; Bộ định thời gian hẹn giờ =new Timer (); timer.schedule (timertask, 0, 10000); } @Override được bảo vệ void onStop () {super.onStop (); LocalBroadcastManager.getInstance (this) .unregisterReceiver (broadcastReceiver); }} 

Tạo một lớp được gọi là tệp service.class và thêm mã sau -

 package com.example.andy.myapplication; nhập android.app.IntentService; nhập android.content.Intent; nhập android.os.IBinder; nhập android.support.v4.content.LocalBroadcastManager; dịch vụ lớp công khai mở rộng IntentService { public static static boolean shouldStop =false; dịch vụ công cộng () {super (service.class.getSimpleName ()); } @Override public IBinder onBind (Ý định có ý định) {return null; } @Override được bảo vệ void onHandleIntent (Ý định có ý định) {Intent Ý định1 =new Intent ("com.example.andy.myapplication"); for (int i =0; i <10; i ++) {Ý định1.putExtra ("someName", "Tutorialspoint.com" + i); LocalBroadcastManager.getInstance (this) .sendBroadcast (ý định1); thử {Thread.sleep (1000); } catch (InterruptException e) {e.printStackTrace (); }} if (shouldStop) {stopSelf (); trở về; }}} 

Bước 4 - Thêm mã sau vào tệp kê khai.xml

              

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm thế nào để cập nhật tiếp tục ui từ Dịch vụ Intent trong Android?

Làm thế nào để cập nhật tiếp tục ui từ Dịch vụ Intent trong Android?

Làm thế nào để cập nhật tiếp tục ui từ Dịch vụ Intent trong Android?

Trong kết quả trên, nó đang tiếp tục cập nhật ui từ dịch vụ mục đích.