在Android中自动启动后会触发通知

时间:2016-10-27 03:40:26

标签: android notifications broadcastreceiver alarmmanager

我是Android开发人员的新手,我的应用有问题。我创建了一个自定义BroadcastReceiver,但是当我启动我的应用时,这会自动运行我的通知。在我的AlarmManager计划开始之前,我不希望这个通知一直在运行。

我真的需要帮助,因为这个应用程序是我的学期论文。

以下是我的代码:

通知接收方:

public class NotificationReceiver extends BroadcastReceiver {
    String cnh_vencida;

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("ALARME_DISPAROU")) {

            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            cnh_vencida = context.getString(R.string.cnh_vencimento);

            PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, CadastroUser.class), 0);

            Notification notification = new NotificationCompat.Builder(context)
                    .setTicker("teste")
                    .setCategory(Notification.CATEGORY_ALARM)
                    .setSmallIcon(R.mipmap.car)
                    .setContentTitle("CarMaintenance")
                    .setContentText(cnh_vencida)
                    .setContentIntent(pi)
                    .setAutoCancel(true)
                    .setSound(uri)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                    .build();
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notification);

        }
    }
}

我的AndroidManifest的一部分

    <receiver
        android:name=".notification.AlarmReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name=".notification.NotificationReceiver"
        android:exported="false"
        android:enabled="false">
        <intent-filter>
            <action android:name="ALARME_DISPAROU" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
</application>

AlarmReceiver(如果用户重启设备,此课程将重新安排闹钟)

public class AlarmReceiver extends BroadcastReceiver {
    String dataValidade;
    Date date;
    String CATEGORIA;

    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            recreateAlert(context);
        }
    }

    public void recreateAlert(Context context) {

        CadastroUserDAO cadastroUserDAO = new CadastroUserDAO(context);

        List<CadastroUserModel> registros;
        registros = cadastroUserDAO.listarTodos();
        if (registros.size() > 0) {
            Log.i(CATEGORIA, "Existe CNH Cadastrada");

            for (int i = 0; i < registros.size(); i++) {
                dataValidade = (registros.get(i).getVALIDADE_CNH());
            }

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date = simpleDateFormat.parse(dataValidade);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(calendar.DAY_OF_MONTH, -45);

            AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent alarmIntent = new Intent("ALARME_DISPAROU");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
            alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 5, pendingIntent);
        }
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

其中一个选项是摆脱'Unable to read from memory'并直接在意图中指定 HRESULT CaptureSample() { HRESULT hr = S_OK; WINBIO_SESSION_HANDLE sessionHandle = NULL; WINBIO_UNIT_ID unitId = 0; WINBIO_REJECT_DETAIL rejectDetail = 0; PWINBIO_BIR sample = NULL; SIZE_T sampleSize = 0; // Connect to the system pool & opening session code done successfully. // Capture a biometric sample. std::cout << "Calling WinBioCaptureSample - Swipe sensor...\n"; hr = WinBioCaptureSample( sessionHandle, WINBIO_NO_PURPOSE_AVAILABLE, WINBIO_DATA_FLAG_RAW, &unitId, &sample, // Here Application crashes and error is 'Unable to read from memory' &sampleSize, &rejectDetail ); std::cout << "Swipe processed - Unit ID: " << unitId << "\n"; if(FAILED(hr)) { if(hr == WINBIO_E_BAD_CAPTURE) std:: cout << "Bad capture; reason: " << rejectDetail << "\n"; else std::cout << "WinBioCaptureSample failed.hr = 0x" << std::hex << hr << std::dec << "\n"; if(sample != NULL) { WinBioFree(sample); sample = NULL; } if(sessionHandle != NULL) { WinBioCloseSession(sessionHandle); sessionHandle = NULL; } return hr; } }

in Manifest:

<intent-filter>
在AlarmReceiver中

NotificationReceiver

并删除NotificationReceiver文件中的<receiver android:name=".notification.NotificationReceiver" /> 语句。