吐血,测一次要等好久

解决方案 »

  1.   

    以开机广播为例,你在接收到广播时发送一个通知到状态栏,如果有该通知,则表明你的开机广播接收成功public class BootBroadcastReceiver extends BroadcastReceiver {    private static final String TAG = "BootBroadcastReceiver";
        private static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";    @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ACTION_BOOT)) { 
                Log.d(TAG, "开机启动完成");
                myNotify(context);
            }
        }    /**
         * 发送一个通知
         */
        private void myNotify(Context context) {
            NotificationManager nm = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            PendingIntent pendingIntent = null;
            Intent it = null;
            it = new Intent(context, MainActivity.class);
            pendingIntent = PendingIntent.getActivity(context, 0,
                    it, PendingIntent.FLAG_ONE_SHOT);
            Notification baseNF = new Notification.Builder(context)
                    .setAutoCancel(true)
                    .setContentTitle("开机广播")
                    .setContentText("测试开机广播")
                    .setTicker("开机广播")
                    .setDefaults(
                            Notification.DEFAULT_SOUND
                                    | Notification.DEFAULT_VIBRATE)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setWhen(System.currentTimeMillis()).build();
            nm.notify(0, baseNF);
        }
    }