пример часового пояса не работает в эмуляторе - PullRequest
1 голос
/ 01 сентября 2010
<receiver android:name=".ExampleBroadCastReceiver" 
    android:enabled="true"> 
    <intent-filter> 
        <action android:name="android.intent.action.ACTION_TIMEZONE_CHANGED"/> 
    </intent-filter> 
</receiver> 
package com.broadcastreceiver;

import java.util.ArrayList;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

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

        Log.d("ExmampleBroadcastReceiver", "intent=" + intent); 
        Intent intent1 = new Intent(context,Login.class); 
        context.startActivity(intent1); 
    } 
} 

Я запускаю приведенный выше код изменения часового пояса в настройках, не вызывая других действий. Кто-нибудь может сказать, в чем проблема?

Ответы [ 3 ]

3 голосов
/ 29 мая 2011

У меня возникла та же проблема, и эта ветка помогла мне заставить работать обновления TimeZone, однако я все еще не получал уведомления об изменениях даты / времени. Я наконец обнаружил, что есть разница в том, что вы указываете в файле манифеста и что ваш широковещательный приемник использует при фильтрации намерений. Хотя это IS задокументировано в справочнике по Android-намерениям, его очень легко не заметить!

В вашем файле AndroidManifest.xml используйте следующее:

  <receiver android:name=".MyReceiver">
    <intent-filter>
      <!-- NOTE: action.TIME_SET maps to an Intent.TIME_CHANGED broadcast message -->
      <action android:name="android.intent.action.TIME_SET" /> 
      <action android:name="android.intent.action.TIMEZONE_CHANGED" />
      <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
  </receiver>

А в классе получателя:

public class MyReceiver extends BroadcastReceiver {
  private static final String TAG = "MyReceiver";
  private static boolean DEBUG = true;

  @Override
  public void onReceive(Context context, Intent intent) {
    final String PROC = "onReceive";

    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        if (DEBUG) { Log.v(TAG, PROC + ": ACTION_BOOT_COMPLETED received"); }
    }
    // NOTE: this was triggered by action.TIME_SET in the manifest file!
    else if (intent.getAction().equals(Intent.ACTION_TIME_CHANGED)) {
      if (DEBUG) { Log.v(TAG, PROC + ": ACTION_TIME_CHANGED received"); }
    }
    else if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
      if (DEBUG) { Log.v(TAG, PROC + ": ACTION_TIMEZONE_CHANGED received"); }
    }
  }

}
1 голос
/ 17 марта 2011

Это не сработало, потому что намерения неверны. Замените вышеуказанные на:

<intent-filter>
    <action android:name="android.intent.action.TIMEZONE_CHANGED" />
    <action android:name="android.intent.action.TIME" />
</intent-filter>

Затем перейдите в область «Настройки» и измените часовой пояс.

0 голосов
/ 01 сентября 2010

Полагаю, вы не включили следующие строки в свой AndroidManifext.xml, не так ли?

 <receiver android:name=".ExampleBroadcastReceiver" android:enabled="false">
     <intent-filter>
         <action android:name="android.intent.ACTION_TIMEZONE_CHANGED" />
         <action android:name="android.intent.ACTION_TIME" />
     </intent-filter>
 </receiver>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...