Java android:后台手机状态监控
我正在尝试创建一个后台进程来监视手机的状态,并在状态发生变化时显示toast消息。到目前为止,我已经实现了这些,但是当我安装并运行时。它什么也不做。它不会显示任何东西。我在这里张贴我的代码。请帮帮我 StateMonitor.java ` PhoneReceiver.javaJava android:后台手机状态监控,java,android,broadcastreceiver,telephony,Java,Android,Broadcastreceiver,Telephony,我正在尝试创建一个后台进程来监视手机的状态,并在状态发生变化时显示toast消息。到目前为止,我已经实现了这些,但是当我安装并运行时。它什么也不做。它不会显示任何东西。我在这里张贴我的代码。请帮帮我 StateMonitor.java ` PhoneReceiver.java `import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; impor
`import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
//Automatically called when State Change is Detected because this Receiver is Registered for PHONE_STATE intent filter in AndroidManifest.xml
public class PhoneStateReceiver extends BroadcastReceiver {
TelephonyManager manager;
PhoneStateMonitor phoneStateListener;
static boolean isAlreadyListening=false;
//This Method automatically Executed when Phone State Change is Detected
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
phoneStateListener =new PhoneStateMonitor(context);//Creating the Object of Listener
manager=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);//Getting the Telephony Service Object
if(!isAlreadyListening)//Checking Listener is Not Registered with Telephony Services
{
manager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);//Registering the Listener with Telephony to listen the State Change
isAlreadyListening=true; //setting true to indicate that Listener is listening the Phone State
}
}
}
`
Android Manifest.xml
`<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pavan.phonecall"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /><!-- Permission for Reading Phone State -->
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- If this app is not running then this will be automatically called when phone state change detected(Look at Intent Filter) -->
<receiver android:name=".PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>`
`
`
当我从eclipse安装时,它显示apk已安装。当我打电话时,祝酒词不会弹出。我哪里做错了?请帮忙!谢谢 安装应用程序时,它将保持在
停止
状态,直到您使用某些UI控件(即活动
)启动应用程序。从年起,它成为强制性的
因此,最终,您需要在应用程序中至少有一个活动
,而不仅仅是广播接收器
,才能将应用程序移出该停止
状态。否则它将不会接收任何广播
参考资料:
那么,如何为相同的应用程序编写后台服务?在我安装之后,它应该只是监视。隐藏的活动或窗口可能是?您可以为此使用相同的代码,但您需要的是至少一个可以启动应用程序的
活动
。那么如何隐藏我的活动?@PavanKumar:您可以尝试为应用程序创建小部件
,并尝试使用它将应用程序移出停止状态。
`<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pavan.phonecall"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /><!-- Permission for Reading Phone State -->
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- If this app is not running then this will be automatically called when phone state change detected(Look at Intent Filter) -->
<receiver android:name=".PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>`