如何将手机状态作为服务知晓?

huangapple go评论88阅读模式
英文:

How to know Phone State as a service?

问题

The Problem:

我希望应用程序能够在后台检查手机的当前通话状态。为了创建一个能够在后台运行的应用程序,我们需要一个服务(源代码在下一节中),但我唯一成功的方式是使用一个接收器。接收器的问题是,如果你将应用程序在后台关闭,它并不总是运行。现在这似乎是一个相当简单的问题,但我对JAVA不熟悉。

Source Codes:

这是我的主要服务的OnStartCommand文件:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setContentTitle("asdaff")
        .setContentText("asdas")
        .setSmallIcon(R.drawable.ic_android_black_24dp)
        .build();
    startForeground(1, notification);
    TelephonyManager telephonyManager =
            (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    PhoneStateListener callStateListener;
    callStateListener = new PhoneStateListener() {
        public void onCallStateChanged(int state, String incomingNumber) {
            String stateString = "N/A";
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    stateString = "Idle";
                    Toast toast = Toast.makeText(getApplicationContext(), stateString, Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER, 0,0);
                    toast.show();
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    stateString = "Off Hook";
                    Toast toast1 = Toast.makeText(getApplicationContext(), stateString, Toast.LENGTH_LONG);
                    toast1.setGravity(Gravity.CENTER, 0,0);
                    toast1.show();
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    stateString = "Ringing";
                    Toast toast2 = Toast.makeText(getApplicationContext(), stateString, Toast.LENGTH_LONG);
                    toast2.setGravity(Gravity.CENTER, 0,0);
                    toast2.show();
                    break;
            }
        }
    };
    return super.onStartCommand(intent, flags, startId);
}

Edit:

使用startForegroundservice()仍然不起作用。我已经获取了电话权限。如果需要更多源代码来解决问题,请告诉我。

英文:

The Problem:

What I want the app to do is checking a phone's current call state in the background. Now, for making an app that works in background we need a service(source codes in the next section), but the only way I have managed to do this is by using a receiver. The issue with the receiver is that it is not always running if you kill the app in the background. Now this seems a fairly simple question, but I am new to JAVA.

Source Codes

This is my main service's OnStartCommand file:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID).setContentTitle("asdaff").setContentText("asdas").setSmallIcon(R.drawable.ic_android_black_24dp).build();
startForeground(1, notification);
TelephonyManager telephonyManager =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener callStateListener;
callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
Toast toast = Toast.makeText(getApplicationContext(), stateString, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0,0);
toast.show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
Toast toast1 = Toast.makeText(getApplicationContext(), stateString, Toast.LENGTH_LONG);
toast1.setGravity(Gravity.CENTER, 0,0);
toast1.show();
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
Toast toast2 = Toast.makeText(getApplicationContext(), stateString, Toast.LENGTH_LONG);
toast2.setGravity(Gravity.CENTER, 0,0);
toast2.show();
break;
}
}
};
return super.onStartCommand(intent, flags, startId);
}

Edit

Used startForegroundservice() and it still does not work. I have the phone permission. Please tell if you need more of the source code to tell the problem.

答案1

得分: 0

所以我找到了解决方法。问题出在onStartCommand方法本身。当使用电话状态监听器时,您需要将监听器注册到电话管理器。

telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

现在,我认为这是那种可以在Google上搜索到的东西,它应该有一个适当的初学者错误警告,可以帮助任何打算犯这样一个愚蠢错误(比如我)的人。

我写下这个答案,以便任何遇到这个问题的人都知道不应该做什么。
干杯。

英文:

So I figured it out. The issue is in the onStartCommand method itself. When using a phone state listener, you need to have the listener registered to the telephony manager.

telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

Now, I thought this is one of those "Google-able" Things, that have a proper beginner mistake warning that may help anyone who is going to commit such a stupid mistake(i.e, me).

I am Writing this answer so that anyone who comes across this may no what not to do.
Cheers.

huangapple
  • 本文由 发表于 2020年8月12日 12:10:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63369646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定