• 26-01-2020, 12:55:20
    #1
    merhaba arkadaşlar arka plan da çalışan servisim var bu servis içerisinde webviews çalıtırmak istiyorum fakat

    final WebView url2 = (WebView) findViewById(R.id.we2);
    burada hata alıyorum sebep nedir nasıl düzelte bilirim?
  • 26-01-2020, 12:58:29
    #2
    Arkaplanda çalışan servisin Layout componenti olmadığı için dolayısıyla findViewById çalışmayacaktır. Ancak notification olarak birşeyler yapılabilir.
  • 26-01-2020, 13:00:02
    #3
    Dogukan adlı üyeden alıntı: mesajı görüntüle
    Arkaplanda çalışan servisin Layout componenti olmadığı için dolayısıyla findViewById çalışmayacaktır. Ancak notification olarak birşeyler yapılabilir.
    hocam android kodlamayı yeni yenibaşladım pek bir bilgim yok varsaörnek kodlamanız beni yönlendire bilirmisiniz
  • 26-01-2020, 13:00:35
    #4
    Üyeliği durduruldu
    Service class'ında direkt olarak view'a erisemezsiniz.

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.layout_adi, null);
    final WebView url2 = (WebView) layout.findViewById(R.id.we2);
    Sanırım çalışacaktır.
  • 26-01-2020, 13:06:25
    #5
    awoken adlı üyeden alıntı: mesajı görüntüle
    Service class'ında direkt olarak view'a erisemezsiniz.

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.layout_adi, null);
    final WebView url2 = (WebView) layout.findViewById(R.id.we2);
    Sanırım çalışacaktır.
    hocam bu kodu nereye yapıştırayim
    package com.example.futboll;import android.app.Notification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.app.Service;import android.content.Context;import android.content.Intent;import android.os.Build;import android.os.IBinder;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Toast;import androidx.core.app.NotificationCompat;public class HelloService extends Service {    @Override    public IBinder onBind(Intent ıntent) {        return null;    }    @Override    public void onCreate() {        super.onCreate();        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);            String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";                        NotificationChannel notificationChannel = new NotificationChannel(                    NOTIFICATION_CHANNEL_ID, "My app no sound", NotificationManager.IMPORTANCE_LOW            );            notificationChannel.setDescription("");            notificationChannel.setSound(null,null);            notificationChannel.enableLights(false);            notificationChannel.enableVibration(false);            notificationManager.createNotificationChannel(notificationChannel);            Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)                    .setContentTitle("")                    .setContentText("").build();            startForeground(1, notification);        }    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Toast.makeText(this, "Servis başlatıldı", Toast.LENGTH_LONG).show();             final WebView url2 = (WebView) findViewById(R.id.we2);        url2.getSettings().setBuiltInZoomControls(true); //zoom yapılmasına izin verir        url2.getSettings().setSupportZoom(true);        url2.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);        url2.getSettings().setAllowFileAccess(true);        url2.getSettings().setDomStorageEnabled(true);        url2.getSettings().setJavaScriptEnabled(true);        url2.setWebViewClient(new WebViewClient());        url2.loadUrl("https://www.youtube.com/watch?v=y9tC1rLDp4w");        return START_NOT_STICKY;    }    @Override    public void onDestroy() {        super.onDestroy();        Toast.makeText(this, "Servis durduruldu", Toast.LENGTH_LONG).show();        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            stopForeground(true);        }    }}