Application.onCreate()上的BroadcastReceiver

时间:2012-03-03 22:30:18

标签: android-intent android-widget broadcastreceiver android

我正在尝试做一个小部件,它会对ACTION_SCREEN_ON/_OFF做出反应。我试图在清单中声明BroadcastReceiver,但它没有用。在某处我读到它应该在代码中注册。我写道:

    public class TimeTableApp extends Application {

    public void OnCreate() {
       Log.d("FinWi","appOncreate");
       Context cnt = this.getApplicationContext();
       IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
       intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
       intentFilter.addAction(Intent.ACTION_DATE_CHANGED);
       BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver();
       cnt.registerReceiver(mReceiver, intentFilter);
       super.onCreate();
    }
  } 

    <application android:name=".TimeTableApp"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

     <receiver android:name=".TimeTableWidgetProvider">
        <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/appwidget_provider" />
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
     </receiver>

    </application>`

它不会调用onCreate()

1 个答案:

答案 0 :(得分:0)

如果这是你一直在尝试的确切代码,那么问题是你在onCreate方法上完全没错。 它应该是这样的(应该有@Override,onCreate以“o”而不是“O”开头):

 @Override
    public void onCreate()
    {
           .
           .
           .