进程间通信

发送方

 //发送广播
            String broadcastIntent = "com.customs.broadcast";//自己自定义
            Intent intent = new Intent(broadcastIntent);
            intent.setComponent(new ComponentName("cn.xender.xwallet",   "cn.xender.xwallet.CustomBroadReceiver"));
            MainActivity.this.sendBroadcast(intent);

接收方

Manifest


            
                
            



CustomBroadReceiver

public class CustomBroadReceiver extends BroadcastReceiver {
    private static final String ACTION = "com.customs.broadcast";

    @Override
    public void onReceive(Context context, Intent intent) {
        //广播接受
        if (ACTION.equals(intent.getAction())) {
            doService(context);
        }
    }

    private void doService(Context context) {
        JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
        JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(context, MyJobService.class));  //指定哪个JobService执行操作
        builder.setMinimumLatency(TimeUnit.MINUTES.toMillis(5)); //执行的最小延迟时间
        builder.setOverrideDeadline(TimeUnit.MINUTES.toMillis(15));  //执行的最长延时时间
        jobScheduler.schedule(builder.build());
    }

}

MyJobService

public class MyJobService extends JobService {
    public static final String TAG = MyJobService.class.getSimpleName();

    @Override
    public boolean onStartJob(JobParameters params) {
        Log.i(TAG, "onStartJob:" + params.getJobId());
        showNotification(getApplicationContext());
        jobFinished(params, false);//任务执行完后记得调用jobFinsih通知系统释放相关资源
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.i(TAG, "onStopJob:" + params.getJobId());
        return false;
    }


    private void showNotification(Context context) {
        /**
         *  创建通知栏管理工具
         */

        NotificationManager notificationManager = (NotificationManager) context.getSystemService
                (NOTIFICATION_SERVICE);

        /**
         *  实例化通知栏构造器
         */

        NotificationCompat.Builder mBuilder;

        //判断是否是8.0Android.O
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel chan1 = new NotificationChannel("static", "Primary Channel", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(chan1);
            mBuilder = new NotificationCompat.Builder(context, "static");
        } else {
            mBuilder = new NotificationCompat.Builder(context);
        }

        /**
         *  设置Builder
         */
        //设置标题
        mBuilder.setContentTitle("我是标题")
                //设置内容
                .setContentText("我是内容")
                //设置大图标
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.xw_icon_launcher))
                //设置小图标
                .setSmallIcon(R.mipmap.xw_icon_launcher)
                //设置通知时间
                .setWhen(System.currentTimeMillis())
                //首次进入时显示效果
                .setTicker("我是测试内容")
                //设置通知方式,声音,震动,呼吸灯等效果,这里通知方式为声音
                .setDefaults(Notification.DEFAULT_SOUND);

        //绑定intent,点击图标能够进入某activity
        Intent mIntent = new Intent(context, MainActivity.class);
        PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(mPendingIntent);

        //发送通知请求
        notificationManager.notify(10, mBuilder.build());
    }
}

你可能感兴趣的:(进程间通信)