我想在服务中添加BroadCastReciverBroadCastReceiver收听电话。我的问题是广播无法在服务中运行(当我运行该应用程序时它起作用了,但是当我退出应用程序时它就不起了作用)。

MainActiviy.java:

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   startService(new Intent(MainActivity.this,FirstService.class));

  }
}


PhoneStateBrodcastRecevier.java:

public class PhoneStateBrodcastRecevier extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {

    try {
      TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
      MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener(context);
      telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    } catch (Exception e){}
  }
}


FirstService.java:

public class FirstService extends Service  {

  BroadcastReceiver myreciReceiver;

  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    myreciReceiver=new PhoneStateBrodcastRecevier();
    IntentFilter intentFilter=new IntentFilter();
    intentFilter.addAction("android.intent.action.PHONE_STATE");
    registerReceiver(myreciReceiver, intentFilter);
  }

  @Override
  public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Log.d("ddddddd", "start");

  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d("ddddddd", "finish");
  }
}


而我的清单:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<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" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

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

    <service android:name=".FirstService"/>
</application>

最佳答案

当您的目标是监听电话状态时,在Service类本身中进行更改。您可以尝试在Service内部进行定义。这也将减少资源使用,因为系统不会负担其他组件的生命周期。在您的Service中:

private final PhoneStateListener mPhoneListener = new PhoneStateListener() {
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

            // Call receive state
            if (state != TelephonyManager.CALL_STATE_IDLE) {
                // Do something
            }
        }
    };


然后,您可以将其注册为收听(可能在onStartCommand()中):

TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);


然后取消注册以免监听onDestroy()(或在完成操作后):

mTelephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_NONE);

关于android - Android服务中的BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38866785/

10-09 00:17