Android 收听电话状态变化的广播接收器

Android 收听电话状态变化的广播接收器,android,broadcastreceiver,alarmmanager,Android,Broadcastreceiver,Alarmmanager,我现在有一个报警管理器和一个广播接收器,每天在某个时间发送通知。目前,每次打电话时,通知都会被激活,我相信这是广播接收器在监听电话状态的变化,但我不知道如何阻止它监听电话状态的变化。下面是我的代码: public class Alarm_Receiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (return_cow_id.isEmp

我现在有一个报警管理器和一个广播接收器,每天在某个时间发送通知。目前,每次打电话时,通知都会被激活,我相信这是广播接收器在监听电话状态的变化,但我不知道如何阻止它监听电话状态的变化。下面是我的代码:

public class Alarm_Receiver extends BroadcastReceiver {

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




if (return_cow_id.isEmpty()){
}else{
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
        new Intent(context, Cows_On_Heat.class), 0);

   NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Cow/s due back on heat today: ")
        .setContentText(return_cow_id);
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}

if (calve_cow_id.isEmpty()){


   }else{



    PendingIntent contentIntent1 = PendingIntent.getActivity(context, 0, 
            new Intent(context, Cows_Calving.class), 0);

   NotificationCompat.Builder mBuilder1 =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Cow/s due to calve in 7 days: ")
            .setContentText(calve_cow_id);
    mBuilder1.setContentIntent(contentIntent1);
    mBuilder1.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder1.setAutoCancel(true);
    NotificationManager mNotificationManager1 =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager1.notify(2, mBuilder1.build());
    }
}
}
任何帮助都将不胜感激。

请查看此链接

使用该教程中的方法来侦听电话状态,并在PhoneStateListener子类中使用布尔变量来控制您的return\u cow\u id.isEmpty()&calve\u cow\u id.isEmpty(),如果条件如下:

public class MyPhoneStateListener extends PhoneStateListener {
    public void onCallStateChanged(int state,String incomingNumber){
        switch(state){
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d("DEBUG", "IDLE");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("DEBUG", "OFFHOOK");
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d("DEBUG", "RINGING");
                break;
        }
        phone_state_changed = true; //here is your phone state controller variable 
    } 
}
那么你的接收器应该是这样的:

if (return_cow_id.isEmpty()){
}else{
    if (!phone_state_changed){
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
        new Intent(context, Cows_On_Heat.class), 0);
        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Cow/s due back on heat today: ")
            .setContentText(return_cow_id);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
        phone_state_changed = false; //now it will work as a toggler
    }
}
if (calve_cow_id.isEmpty()){
}else{
    if(!phone_state_changed){
        PendingIntent contentIntent1 = PendingIntent.getActivity(context, 0, 
            new Intent(context, Cows_Calving.class), 0);

        NotificationCompat.Builder mBuilder1 =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Cow/s due to calve in 7 days: ")
            .setContentText(calve_cow_id);
        mBuilder1.setContentIntent(contentIntent1);
        mBuilder1.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder1.setAutoCancel(true);
        NotificationManager mNotificationManager1 =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager1.notify(2, mBuilder1.build());
        phone_state_changed = false; //now it will work as a toggler
    }
}
}

希望我帮助了,如果我愿意,请考虑放弃一个被接受的答案,如果没有,然后毫不犹豫地问。

谢谢你的帮助,但我发现了问题。我将该操作定义为电话状态更改。所以我所做的就是创建一个自定义的意图和行为