当应用程序关闭Lollipop版本时,BroadCastReciver for phone状态无效

时间:2017-02-21 11:57:58

标签: android android-5.0-lollipop android-broadcastreceiver

PHONE_STATE的我的广播接收器适用于kitkatLollipop版本,即使应用已关闭,但当我使用Lollipop版本时应用已关闭

这是我的清单文件

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>



        <receiver android:name=".BlockCallReceiver" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>



    </application>

2 个答案:

答案 0 :(得分:1)

默认情况下,一些新的Android设备有安全应用程序。有时候这些应用会锁定你的自动启动模式你可以检查它可能阻止启动广播接收器的设置吗?

答案 1 :(得分:1)

创建一个新应用并尝试此操作;

广播接收器

  public class PhoneStateReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String str = intent.getAction();
    if ("android.intent.action.PHONE_STATE".equals(str))
        inComing(context, intent);

    if ("android.intent.action.NEW_OUTGOING_CALL".equals(str))
        outGoing(context, intent);
  }

 private void inComing(Context context, Intent intent){
    String callState = intent.getStringExtra("state");
    if ("RINGING".equals(callState)){
        Log.i(TAG, "RINGING SENDS BUSY");
    }else if ("OFFHOOK".equals(callState)){
        Log.i(TAG, "OFFHOOK SENDS BUSY");
    }else if("IDLE".equals(callState)){
        Log.i(TAG, "IDLE SENDS AVAILABLE"); 
    }
  }

 private void trueCallerOutgoing(Context context, Intent intent)
  {
    String callState = intent.getStringExtra("state");
    if ("RINGING".equals(callState)){
            Log.i(TAG, "RINGING SENDS BUSY");
    }else if ("OFFHOOK".equals(callState)){
            Log.i(TAG, "OFFHOOK SENDS BUSY");
    }else if("IDLE".equals(callState)){
            Log.i(TAG, "IDLE SENDS AVAILABLE"); 
    }
  }
}

<强>清单

 <receiver android:name="PhoneStateReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

并且不要忘记许可

  <uses-permission android:name="android.permission.READ_PHONE_STATE" />