2012-03-03 76 views
0

我正在嘗試做一個小部件,它會對ACTION_SCREEN_ON/_OFF做出反應。我試圖在清單中聲明BroadcastReceiver,但它不起作用。我讀過的地方應該用代碼註冊。我寫道:BroadcastReceiver on Application.onCreate()

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()

+0

的onCreate()......但在OnCreate().. – rusCrypto 2012-03-04 07:36:19

+0

其他一切正常 – rusCrypto 2012-03-04 07:36:35

+0

你實際上並沒有重寫oncreate,因爲它需要BUndle作爲參數,,,, – 2012-03-04 19:50:49

回答

0

如果這是你一直在嘗試的確切代碼,那麼問題是你已經全部錯誤的onCreate方法。 這是應該的樣子(應該有@覆蓋,並用的onCreate 「O」 開頭,而不是 「O」):

@Override 
    public void onCreate() 
    { 
      . 
      . 
      .