2012-09-07 48 views
2

Android應用程序只能播放接收器和服務而沒有活動嗎? 如果這是可能的我怎麼能調用廣播接收器? Android系統會自動調用broadcsat接收器?廣播接收器Android應用程序只能播放接收器和服務而沒有活動

public class CheckReceiver extends BroadcastReceiver { 

     public Context con; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 

      Toast.makeText(context, "Received", Toast.LENGTH_LONG).show(); 
      // add PhoneStateListener 
      PhoneCallListener phoneListener = new PhoneCallListener(); 
      TelephonyManager telephonyManager = (TelephonyManager) context 
        .getSystemService(Context.TELEPHONY_SERVICE); 
      telephonyManager.listen(phoneListener, 
        PhoneStateListener.LISTEN_CALL_STATE); 

      con = context; 
           } 

     class PhoneCallListener extends PhoneStateListener { 

      private boolean isPhoneCalling = false; 

      String LOG_TAG = "LOGGING 123"; 

      @Override 
      public void onCallStateChanged(int state, String incomingNumber) { 

       if (TelephonyManager.CALL_STATE_RINGING == state) { 
        // phone ringing 
        Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); 
       } 

       if (TelephonyManager.CALL_STATE_OFFHOOK == state) { 
        // active 
        Log.i(LOG_TAG, "OFFHOOK"); 

        isPhoneCalling = true; 
       } 

       if (TelephonyManager.CALL_STATE_IDLE == state) { 
        // run when class initial and phone call ended, need detect flag 
        // from CALL_STATE_OFFHOOK 
        Log.i(LOG_TAG, "IDLE"); 
        if (isPhoneCalling) { 
         Log.i(LOG_TAG, "restart app"); 
         Intent start = new Intent(con, CheckService.class); 
         con.startService(start); 
         isPhoneCalling = false; 
        } 

       } 
      } 
     } 
    } 

服務守則

代碼是

public class CheckService extends Service{ 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     // TODO Auto-generated method stub 
     super.onStart(intent, startId); 
     Intent dialogIntent = new Intent(CheckService.this,SmartDialog.class); 
     dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(dialogIntent); 


     //Toast.makeText(CheckService.this, "Serive", Toast.LENGTH_LONG).show(); 
    } 

} 

Android清單文件是

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.mkyong.android" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 

    <uses-permission android:name="android.permission.CALL_PHONE" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 

     <receiver android:name="CheckReceiver"></receiver> 
     <service android:name="CheckService"></service> 
     <activity android:name="SmartDialog" android:theme="@android:style/Theme.Dialog"></activity> 
    </application> 

當我嘗試運行這段代碼接收器,無法啓動。任何幫助都將非常有幫助

+0

,如果你不想在你的應用程序的任何活動,你只能有接收器和服務! –

+0

我在上面的代碼中只有接收器和服務本身,誰會調用接收器是問題,android會自動執行它嗎? – Badrinath

+0

你的清單需要被告知何時調用接收器...即INTENT必須被定義! 試試這個:http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html –

回答

8

從Honeycomb開始,BroadcastReceivers將以停止狀態安裝,並且在應用程序實際運行之前不會觸發,即您將需要一個至少運行一次的活動。這是深入解釋這個Commonsware博客文章:

Broadcast Regression Confirmed

Android 3.1

0
public class FlashApkclass extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 

    } 
} 

在你manifest.xml文件設置你的Android:名字像

<receiver android:name="com.example.ambrecentral.server.FlashApkclass" 
       android:exported="false"> 
     <intent-filter> 
     // register you broadcast receiver to list broadcast 
      <action android:name="com.example.android.APP_CLOUD_DELETE_APK"/> 
     </intent-filter> 

</receiver> 

,如果你想播出

broadcast.setAction(BROADCAST_ACTION); 
sendBroadcast(broadcast); 

其中BROADCAST_ACTION是

public static String BROADCAST_ACTION = "com.example.android.APP_CLOUD_DELETE_APK";  
+0

我應該在哪寫這部分代碼? broadcast.setAction(BROADCAST_ACTION); sendBroadcast(廣播); – Badrinath

相關問題