Android Service,BroadCast Receiver Kullanım

Tamamını indir

package com.example.ali.kopyalakaydet.Service;

import android.app.Service;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
import com.example.ali.kopyalakaydet.Database.database;
import java.text.SimpleDateFormat;
import java.util.Date;
public class KopyalaService extends Service {
    ClipboardManager cm;
    SQLiteDatabase db;
    database vt;
    @Nullable
    @Override
    public IBinder onBind(Intent ıntent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        cm = (ClipboardManager) getSystemService(Service.CLIPBOARD_SERVICE);

                    cm.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
                        @Override
                        public void onPrimaryClipChanged() {
                           try {
                               vt = new database(getApplicationContext());
                               db = vt.getWritableDatabase();
                               ClipData cd = cm.getPrimaryClip();
                               String item=cd.getItemAt(0).getText() + "";
                               ContentValues par=new ContentValues();
                               par.put("ITEM",item);
                               SimpleDateFormat frm=new SimpleDateFormat("yyyy-MM-dd");
                               par.put("DATE",frm.format(new Date()));
                               db.insertOrThrow("COPYITEMS",null,par);
                               Toast.makeText(getApplicationContext(),item+" Kayıt Edildi.", Toast.LENGTH_LONG).show();
                               db.close();
                           }
                           catch (Exception ex)
                           {
                               Toast.makeText(getApplicationContext(),"Hata="+ex.getMessage(), Toast.LENGTH_LONG).show();
                           }
                           }
                    });

      return  super.onStartCommand(intent, flags, startId);
    }

}

 

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir