judaco / Android

Basics
1 stars 0 forks source link

service #12

Open judaco opened 7 years ago

judaco commented 7 years ago
package com.example.lesson4_services;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    private int counter = 0;
    private Thread thread;
    private boolean go = true;
    private IBinder binder = new MyBinder();

    MainActivity mainActivity;

    public void setMainActivity(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

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

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("Juda", "service created");
        Toast.makeText(this, "service created", Toast.LENGTH_SHORT).show();
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("Juda", "service started " + counter);
        counter++;
        startThread();
        return START_STICKY;
    }

    private void startThread(){
        if(thread == null){
            go = true;
            thread = new Thread(){
                @Override
                public void run() {
                    while(go) {
                        Log.d("Juda", "thread is running " + counter++);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            thread.start();
        }
    }

    private void stopThread(){
        if(thread != null){
            go = false;
            thread.interrupt();
            thread = null;
        }
    }

    public void stam(){
        Notification.Builder notificationBuilder = new Notification.Builder(this);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentTitle("new notification!");
        notificationBuilder.setContentText("hello");
        notificationBuilder.setAutoCancel(true);
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        notificationBuilder.setLights(0xFFFF0000, 500, 500);
        notificationManager.notify(1, notificationBuilder.build());

    }
}
judaco commented 7 years ago
package com.example.lesson4_services;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    private MyService myService;
    boolean bound = false;

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

        if(!bound){
            //this created the service but doesn't start it.
            Intent intent = new Intent(this, MyService.class);
            bindService(intent, connection, BIND_AUTO_CREATE);
        }
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the service object we can use to
        // interact with the service.  Because we have bound to a explicit
        // service that we know is running in our own process, we can
        // cast its IBinder to a concrete class and directly access it.
            MyService.MyBinder binder = (MyService.MyBinder)service;
            myService = binder.getService();
            bound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        // Because it is running in our same process, we should never
        // see this happen.
            Toast.makeText(MainActivity.this, "disconnected", Toast.LENGTH_SHORT).show();
            bound = false;
        }
    };

    public void btnStopService(View view) {
        if(bound){
            bound = false;
            unbindService(connection);
        }
        if(myService != null)
            myService.stopSelf();
        stopService(new Intent(this, MyService.class));
    }

    public void btnStartService(View view) {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
        if(!bound){
            bindService(intent, connection, BIND_AUTO_CREATE);
        }
    }

    public void btnStam(View view) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(bound){
            bound = false;
            unbindService(connection);// Detach our existing connection.
        }
    }
}
judaco commented 7 years ago
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Service"
        android:onClick="btnStartService"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop Service"
        android:onClick="btnStopService"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stam"
        android:onClick="btnStam"/>

</LinearLayout>