在BroadcastReceiver中启动Service

在BroadcastReceiver中启动Service

package com.qywanwei.fysp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
//        throw new UnsupportedOperationException("Not yet implemented");
        Log.d("MyReceiver", "开机启动");

        Toast.makeText(context,"000",Toast.LENGTH_SHORT).show();

        //  启动一个Service
        Intent serviceIntent = new Intent(context, MyService.class);
        context.startService(serviceIntent);
//        启动一个Activity
        Intent activityIntent = new Intent(context, MainActivity.class);
        //  要想在Service中启动Activity,必须设置如下标志
        activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(activityIntent);
    }
}

  

原文地址:https://www.cnblogs.com/huichao1314/p/6760482.html