package com.siwei.recyclebox.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.siwei.recyclebox.ui.main.MainActivity;

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Let it continue running until it is stopped.
//        Toast.makeText(this, "服务已经启动", Toast.LENGTH_LONG).show();
        Log.i("server","服务已经启动");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {

//            Intent intent=new Intent(this,MyService.class);
//            this.startService(intent);

        Intent mStartActivity = new Intent(this, MainActivity.class);
        int mPendingIntentId = 123456;
        PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
        System.exit(0);
        super.onDestroy();
//        Toast.makeText(this, "服务已经停止", Toast.LENGTH_LONG).show();
//        Log.i("","服务已经停止");
    }
}