【问题标题】:BroadcastReceiver on Application.onCreate()Application.onCreate() 上的广播接收器
【发布时间】:2012-03-03 22:30:17
【问题描述】:

我正在尝试做一个小部件,它将对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()

【问题讨论】:

  • onCreate().... 但是是 OnCreate()...
  • 其他一切正常
  • 你实际上并没有覆盖 oncreate 因为它需要 BUndle 作为参数 ,,,,

标签: android-intent android-widget broadcastreceiver android


【解决方案1】:

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

 @Override
    public void onCreate()
    {
           .
           .
           .

【讨论】:

    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多