2014-12-26 92 views
-1

我正在尝试创建一个后台进程,用于监控电话状态并在状态发生变化时显示Toast消息。到目前为止,我实施了这些,但是当我安装并运行。它什么也没做。它不会显示任何东西。我在这里发布我的代码。请帮助我。android:后台电话状态监控

StateMonitor.java

`

import android.content.Context; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.widget.Toast; 

public class PhoneStateMonitor extends PhoneStateListener { 
    Context context; 

    public PhoneStateMonitor(Context context) { 
     super(); 
     // TODO Auto-generated constructor stub 
     this.context=context; 
    } 

    //This Method Automatically called when changes is detected in Phone State 
    public void onCallStateChanged(int state, String incomingNumber) { 
     // TODO Auto-generated method stub 
     super.onCallStateChanged(state, incomingNumber); 

     Toast.makeText(context, "Phone State - "+state+" Incoming Number - "+incomingNumber, Toast.LENGTH_LONG).show();//Giving the Message that Phone State Changed 
     //Checking The phone state 
     switch(state) 
     { 
     case TelephonyManager.CALL_STATE_IDLE: //Phone is in Idle State 
      Toast.makeText(context, "Phone State is IDLE", Toast.LENGTH_LONG).show(); 
      break; 
     case TelephonyManager.CALL_STATE_RINGING: //Phone is Ringing 
      Toast.makeText(context, "Phone State is RINGING", Toast.LENGTH_LONG).show(); 
      break; 
     case TelephonyManager.CALL_STATE_OFFHOOK: //Call is Received 
      Toast.makeText(context, "Call State is OFFHOOK",Toast.LENGTH_LONG).show(); 
      break; 
     } 
    } 
} 
` 

PhoneReceiver.java

`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 。当我拨打我的电话时,Toast消息不会弹出。我哪里错了?请帮忙!谢谢!

回答

1

当您安装应用程序时,它将保持在stopped状态,直到您通过一些UI控件(即Activity)启动应用程序。它从Android 3.1 APIs (API 12)变成强制性的。因此,最终,除了BroadcastReceiver之外,您的应用程序中至少需要一个Activity,才能将您的应用程序移出stopped状态。否则它不会收到任何广播。

参考:

Launch Controls

+0

那么我怎样写一个相同的后台服务?我安装后,它应该只监视。隐藏的活动或窗口可能是? –

+0

你可以使用相同的代码,但你需要的是至少一个可以启动你的应用程序的Activity。 –

+0

那么我如何隐藏我的活动? –