android绑定服务

d8tt03nd  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(202)

我设计了一个类似于bellow的列表视图。我在学安卓,我得到了一份帮助。

---------------------------------------
File Name 1     0%      Download Button
---------------------------------------
File Name 2     0%      Download Button
---------------------------------------
File Name 3     0%      Download Button
---------------------------------------

我有一个使用绑定服务的程序,其中有一个计数器,单击按钮后计数为1%到100%。
现在我可以如何使用这个程序为列表视图的每个项目。
注意:我想为此使用绑定服务。
下面的程序有一个启动服务按钮,可以启动绑定服务并将brodcast发送到ui以显示1%到100%的进度。我必须为我的listview或recyclerview中的每个项目执行此操作。
在我上面的布局,当我点击下载按钮,它应该启动绑定服务,并显示进度。
活动\u main.xml

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="start_service"
    android:text="Start" />

<TextView
    android:id="@+id/showCount"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView" />

主活动.java

public class MainActivity extends AppCompatActivity {
    TextView showCount;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showCount = findViewById(R.id.showCount);

        Intent intent = new Intent(MainActivity.this, MyService.class);
        bindService(intent, sConnection, BIND_AUTO_CREATE);

    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String s1 = intent.getStringExtra("DATAPASSED");
            showCount.setText(s1);
        }
    };

    public void start_service(View view) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(MyService.MY_ACTION);
        registerReceiver(broadcastReceiver, intentFilter);

        myService.startCounting();

        super.onStart();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isBind) {
            unbindService(sConnection);
            isBind = false;
            Log.d(TAG, "onStop: ");
        }
    }

    MyService myService;
    boolean isBind = false;
    public ServiceConnection sConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            MyService.LocalBinder binder = (MyService.LocalBinder) iBinder;
            myService = binder.getService();
            isBind = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            isBind = false;
        }
    };

}

myservice.java文件

public class MyService extends Service {
    private static final String TAG = "MyService";
    static final String MY_ACTION = "MY_ACTION";

    public static final String CHANNEL_ID = "CHANNEL_ID";

    public final IBinder binder = new LocalBinder();

    public class LocalBinder extends Binder {
        MyService getService() {
            return MyService.this;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: start gn");
        return binder;
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: ");
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = "Supper Text";

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Notification Title")
                .setContentText(input)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(0, notification);

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy: ");
        super.onDestroy();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.d(TAG, "onRebind: ");
        super.onRebind(intent);
    }

    public void startCounting() {
        if (!running) {
            running = true;
            new GenerateNumber().start();
        }
    }

    public void stopCounting() {
        if (running) {
            running = false;
        }
    }

    int generatedNumber;
    boolean running;

    class GenerateNumber extends Thread {
        GenerateNumber() {}

        @Override
        public void run() {

            while (running) {
                generatedNumber++;
                Log.d(TAG, "run: " + generatedNumber);
                try {

                    Intent intent = new Intent();
                    intent.setAction(MY_ACTION);
                    intent.putExtra("DATAPASSED", String.valueOf(generatedNumber));
                    sendBroadcast(intent);
                    Thread.sleep(500);

                    if (generatedNumber >= 100) {
                        running = false;
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题