0

私は現在、毎日特定の時刻に通知を送信するためのアラーム マネージャーとブロードキャスト レシーバーを持っています。現在、電話をかけるたびに通知がアクティブになり、これは放送受信機が電話の状態の変化をリッスンしているからだと思いますが、電話の状態の変化をリッスンするのを止める方法がわかりません。以下は私のコードです:

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());
    }
}
}

どんな助けでも大歓迎です。

4

1 に答える 1

0

このリンクを参照してくださいhttp://www.tutorialforandroid.com/2009/01/get-phone-state-when-someone-is-calling_22.html

そのチュートリアルのメソッドを使用して電話の状態をリッスンし、PhoneStateListener サブクラスでブール変数を使用して return_cow_id.isEmpty() & calve_cow_id.isEmpty() if 条件を制御します。

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
    }
}
}

お役に立てば幸いです。そうでない場合は、受け入れられた回答を削除することを検討してください。お気軽にお問い合わせください。

于 2013-11-10T11:21:48.560 に答える