2016-05-28 4 views
0

単純なカウントダウンタイマーアプリケーションを構築しようとしています。 MainActivityはサービス(TimerService)を開始します。このサービスはCountDownTimerを開始します。すべてのチックの後、私はMainActivityで私の意見を更新するために放送を送ります。ビュー時間は最後のティックまで更新されます。 CountDownTimerが終了すると、タイマーが終了したことを示す別のアクションでインテントを作成しています。しかし、その意図はBroadcastReceiverによって受信されていません。インテントアクションを変更した後、ブロードキャストレシーバがインテントを受信して​​いません

これは私のCountDownTimerコードです。

@Override 
    public void onFinish() { 
     Log.i(TAG, "Timer finished"); 
     Intent notifyMainAct = new Intent(Constants.TIME_OVER); 
     notifyMainAct.putExtra(Constants.GET_TIMER_VALUE,String.valueOf(0)); 
     sendBroadcast(notifyMainAct); 
     stopSelf(serviceStartId); 
     Log.i(TAG, "Stopping service " +serviceStartId); 

    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     long minRemaining = millisUntilFinished/60000; 
     Log.i(TAG, "On Tick: "+String.valueOf(minRemaining)); 
     Intent notifyMainAct = new Intent(Constants.BROADCAST_ACTION); 
     notifyMainAct.putExtra(Constants.GET_TIMER_VALUE,String.valueOf(minRemaining)); 
     sendBroadcast(notifyMainAct); 
    } 

これはMainActivityの私のBroadcastReceiverです。

private BroadcastReceiver timerCountReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.i(TAG, "On receiving intent"); 
     String action = intent.getAction(); 
     Log.i(TAG, action); 
     if(action.equalsIgnoreCase(Constants.BROADCAST_ACTION)) { 
      String timerCount = intent.getExtras().getString(Constants.GET_TIMER_VALUE); 
      Log.i(TAG, " Timer count received in Activity "+timerCount); 
      time_remaining.setText(timerCount); 

     } 
     else if(action.equalsIgnoreCase(Constants.TIME_OVER)){ 
      usr_msg.setText("Time OVER"); 
     } 
    } 
}; 

これは私のmanifest.xmlです。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.tony"> 

<uses-permission android:name="android.permission.INTERNET" /> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <service 
     android:name=".TimerService" 
     android:enabled="true" 
     android:exported="false"> 
    </service> 
</application> 

答えて

0

これはonResume()メソッド内BroadcastReceiverを登録中にマイナーthing.Iが意図アクションを登録するには忘れていました。

@Override 
public void onResume(){ 
    super.onResume(); 
    IntentFilter receiverFilter = new IntentFilter(); 
    receiverFilter.addAction(Constants.BROADCAST_ACTION); 
    receiverFilter.addAction(Constants.TIME_OVER_ACTION); 
    registerReceiver(timerCountReceiver, receiverFilter); 

} 
関連する問題