🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / Android Yeni / ezan vakti prodaki gibi hiç kapanmaz telefona reset atsan dahi servis

 

1-) Android Yeni -  ezan vakti prodaki gibi hiç kapanmaz telefona reset atsan dahi servis

ezan vakti prodaki gibi hiç kapanmaz telefona reset atsan dahi servis

UYARI = Eğer OnDestroy() metodundada servisi yine başlatırsan . programı kapatsa dahi kapanmaz :)

1-) AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<application>
...
<service android:name=".HelloService" android:exported="true"  android:launchMode="singleTop" android:process=":my_process"  >
</service>
<receiver
android:name=".BootNotificationReceiver"
android:enabled="true"
android:exported="true"
android:label="BootNotificationReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
2-) HelloService.java 
package com.example.rambo.servisexample;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import static com.example.rambo.servisexample.Services.*;

public class HelloService extends Service {
    Timer timer;
    public static int sayi=1;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {// OnCreate() çalıştıktan sonra her start'da burası çalışır
//TODO do something useful
        Log.d("1","servis çalışıyordu tekrardan tetiklendi onStartCommand()");
        Toast.makeText(getApplicationContext(), "onStartCommand() metodu çalıştır", Toast.LENGTH_LONG).show();
       return Service.START_STICKY;// uygulamayı kapatsan bile arka planda çalışır. 1 kere uygulamayı kapatsan , sonra onCreate 1 kere daha aktif eder. sadece 1 kere OnCreate yapar kapattıktan sonra
       // return Service.START_NOT_STICKY;;// servisi birdaha çalıştırmaz
       // return Service.START_REDELIVER_INTENT;// onCreate'yi çalıştırır. Yani servisi durdurur.
    }
    @Override
    public void onCreate() {// ilk burası çalışır...Servis startService(); metoduyla çağrıldığında çalışır

        Toast.makeText(getApplicationContext(), "Servis onCreate() metodu Çalıştı.", Toast.LENGTH_LONG).show();
        Log.d("1","servis çalıştı onCreate()");
        timer new Timer();
        timer.schedule(new TimerTask() {  //her 60 sn de bir bildirimGonder(); metodu çağırılır.
            @Override
            public void run() {
              Log.d("her","5n");
                noti();
               //Toast.makeText(context, "her 5 sn'de", Toast.LENGTH_LONG).show();
            }

        }, 05000);

    }
    public void  noti(){
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.a)
                        .setContentTitle("My notification"+sayi++)
                        .setContentText("Hello World!");
        Intent resultIntent = new Intent(this, Services.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(Services.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());// buradaki 1 sayisi uyarıda eğer notificat'nin id=1 ise üzerinde güncelleme yapar. değilse 1 tane daha uyarı verir
        startForeground(1, mBuilder.build());// ezan vakti prodaki gibi hiç kapanmaz
    }
    @Override
    public void onDestroy() {
        Log.d("1","servis durdu onDestroy()");
        Toast.makeText(getApplicationContext(), "service done onDestroy() metodu çağırıldı", Toast.LENGTH_SHORT).show();
       /* super.onDestroy();
        sendBroadcast(new Intent("aslakapammam"));*/
    }


    /////////////aşağısı farklı boşver///////////////////////
    @Nullable
    @Override
    public IBinder onBind(Intent ıntent) {
        Log.d("1","onBind() çalıştı");
        Toast.makeText(getApplicationContext(), "service done onBind() metodu çağırıldı", Toast.LENGTH_SHORT).show();
        return null;
    }
    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("1","onUnbind() çalıştı");
        Toast.makeText(getApplicationContext(), "service done onUnbind() metodu çağırıldı", Toast.LENGTH_SHORT).show();
        return false;
    }
    @Override
    public void onRebind(Intent intent) {
        Log.d("1","onRebind() çalıştı");
        Toast.makeText(getApplicationContext(), "service done onRebind() metodu çağırıldı", Toast.LENGTH_SHORT).show();
    }

}
3-) Services.java
package com.example.rambo.servisexample;

import android.app.ActivityManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.provider.SyncStateContract;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import static android.app.Notification.PRIORITY_HIGH;
import com.example.rambo.servisexample.HelloService;

public class Services extends AppCompatActivity {


    Button btn_id;
    TextView txt_id;
    Button btn_calistir;
    Intent intent;

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


        btn_id = (Button) findViewById(R.id.btn_id);
        btn_calistir = (Button) findViewById(R.id.btn_calistir);
        btn_id.setText("Servisi Başlat");

        if (servisCalisiyormu() == true) {//Servis çalışıyorsa
            Toast.makeText(this"servis çalışıyor", Toast.LENGTH_SHORT).show();
            btn_id.setText("Servisi Durdur");
        } else {//Servis çalışmıyorsa
            Toast.makeText(this"servis çalışmıyor", Toast.LENGTH_SHORT).show();
            btn_id.setText("Servisi Başlat");
        }

        btn_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent new Intent(Services.this, HelloService.class);

                if (servisCalisiyormu() == true) {//Servis çalışıyorsa
                    intent.putExtra("ramoKEY1""ramodeger1");
                    stopService(intent);//servisi durdurur
                    btn_id.setText("Servisi Başlat");

                } else {//Servis çalışmıyorsa
                    intent.putExtra("ramoKEY1""ramodeger1");
                    startService(intent);//Servisi başlatır
                    btn_id.setText("Servisi Durdur");


                }
            }
        });
        btn_calistir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService(intent);//Servisi başlatır
            }
        });

        intent new Intent(Services.this, HelloService.class);
        intent.putExtra("ramoKEY1""ramodeger1");
        startService(intent);//Servisi başlatır
        btn_id.setText("Servisi Durdur");


    }
    // olmasada olur

    //olmasada olur

    public boolean servisCalisiyormu() {//Servis Çalışıyor mu kontrol eden fonksiyon
        try {
            ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
                if (HelloService.class.getName().equals(service.service.getClassName())) {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            Toast.makeText(this"servisCalisiyormu() hata " + e.getMessage(), Toast.LENGTH_SHORT).show();
            return false;
        }

    }


 2021 Ocak 18 Pazartesi
 405