2014-11-21 55 views
1

在我的應用程序中,我使用BroadcastReceiver來跟蹤電話狀態。Android:我的應用程序未使用時運行服務

TelephonyManager中有3個可用狀態。

  1. EXTRA_STATE_IDLE
  2. EXTRA_STATE_OFFHOOK
  3. EXTRA_STATE_RINGING

在我的情況我想顯示簡單的用戶界面,顯示有關呼叫者的一些細節,而在電話鈴聲響起,我想關閉當用戶或通話出席的呼叫結束時。

當手機狀態爲振鈴時,我正在撥打我的服務。如果我的應用程序正在使用,那麼當電話響起時,我可以看到我的用戶界面部分。如果我的應用程序不在使用,那麼我不能看到用戶界面部分。

enter image description here

我在後臺已經檢查。該服務沒有運行。我認爲我的服務只在我的應用程序正在使用時運行,並且在我的應用程序未被使用時未運行。

爲了克服這個我該怎麼辦?

這是我的代碼。

@Override 
public void onReceive(Context context, Intent intent) { 
LoginActivity.login.finish(); 
MainActivity.main.finish(); 
telephonyRegister(context,intent); 
} 
public void telephonyRegister(final Context context, Intent intent) 
{ 
TelephonyManager telephony = (TelephonyManager)  context.getSystemService(Context.TELEPHONY_SERVICE); 
String imeiSIM1= telephony.getDeviceId(); 
System.out.println("IMEI NUMBER"+imeiSIM1);       
ctx=context; 
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
     TelephonyManager.EXTRA_STATE_IDLE)) { 
    try 
    { 
    ProjectDailogActivity.projdialog.finish(); 
    }catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    } 
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
     TelephonyManager.EXTRA_STATE_OFFHOOK)) 
{ 
    try{ 
     ProjectDailogActivity.projdialog.finish(); 
     }catch(Exception e) 
     { 
      e.printStackTrace(); 
     }  
} 
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
     TelephonyManager.EXTRA_STATE_RINGING)){ 
    phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 
    isringing=true; 
       Intent intent11 = new Intent(ctx,ProjectDailogActivity.class); 
       intent11.putExtra("incoming_number",phoneNumber); 
       intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       ctx.startActivity(intent11); 

} 

} 

的Manifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<receiver android:name="com.domyhome.broadcastreceiver.IncomingCallInterceptor" > 
     <intent-filter> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
     </intent-filter> 
    </receiver> 

我喜歡truecaller窗口。

只是我提到的值是默認值。

但是當我的應用程序未被使用時,相同的窗口不會來。

請給我一個主意。

回答

0

這就是爲什麼Android開發者發明了BroadcastReciever - 它允許你勾你的代碼作爲服務響應系統事件(如啓動,飛行模式等)

Vogella覆蓋非常好:http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html

和您的具體情況(發送命令作爲手機狀態的響應),您也可以使用本文提供哪一個完全工作例如:http://androidexample.com/Incomming_Phone_Call_Broadcast_Receiver__-_Android_Example/index.php?view=article_discription&aid=61&aaid=86

文章hightlights:

<!-- Manifest.xml --> 
    <receiver android:name=".IncomingCall"> 
      <intent-filter> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
      </intent-filter> 
    </receiver> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> 

,並在Java中 - 添加這些類

//your Listener class: 
    private class MyPhoneStateListener extends PhoneStateListener 
    { 
     public void onCallStateChanged(int state, String incomingNumber) 
     { 
      // state = 1 means when phone is ringing 
      if (state == 1) 
      { 
       // do something 
      } 
     } 
    } 

    //Reciever class: 
    public class IncomingCall extends BroadcastReceiver 
    { 
     public void onReceive(Context context, Intent intent) 
     { 
      // TELEPHONY MANAGER class object to register one listner 
      TelephonyManager tmgr = (TelephonyManager) context 
        .getSystemService(Context.TELEPHONY_SERVICE); 

      //Create Listner 
      MyPhoneStateListener PhoneListener = new MyPhoneStateListener(); 

      // Register listener for LISTEN_CALL_STATE 
      tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE); 
     } 
    } 
0

無需在接收器部分的應用程序的xml中的服務中運行它,使用電話響鈴操作並在接收器中實現您的UI。並有一個不同的接收器讓你的應用在手機切割時收聽。

+0

其實它類似於一個truecaller應用。所以它應該始終在後臺運行。當我的應用程序未被使用時,我可以看到用戶界面。我也試過服務和沒有服務(只是直接從響鈴狀態調用一個活動)。但是當我的應用程序還沒有使用時,我想顯示UI? – 2014-11-21 12:07:55

+0

您可以通過上述方法實現您的要求。您可以定義startactivity(intent(從您的broadcastreciever – therealprashant 2014-11-21 12:09:30

+0

請檢查我的問題,我已更新 – 2014-11-21 12:17:03

0
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // TODO Auto-generated method stub 
    Log.e("onStartCommand", "onStartCommand"); 
    return Service.START_STICKY; 
} 

http://developer.android.com/reference/android/app/Service.html

+0

謝謝迪帕克,請檢查我的更新問題 – 2014-11-21 14:37:35

+0

您需要使用bindservice的概念 – deepak825 2014-11-26 09:27:21

+0

public boolean bindService(Intent service,ServiceConnection conn, int flags){返回mBase.bindService(service,conn,flags); } – deepak825 2014-11-26 09:28:18

0

使用AlarmManager到一個特定的時間後啓動服務和停止服務,也可以殺死AlarmManager時打了進來。

0

嘗試這樣,

AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class); 
intent.putExtra(ONE_TIME, Boolean.FALSE); 

PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); 
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pi); 

,而你手動沒有停止過它,它會重複......可能這可以幫助你...

相關問題