2013-07-30 330 views
0

我在我的應用程序中使用基於鬧鐘的應用程序我已經使用以下代碼設置了鬧鐘。如果我創建鬧鐘意味着創建,但鬧鐘不會移動到廣播接收器。我嘗試了幾次,但它不適合我。即使我已經正確地在android清單文件中添加了廣播接收器。Android廣播接收器

1.設置報警類別:

public class Alertedit extends Activity { 

    boolean AlarmEnabled; 
    final static int RQS_1 = 1; 
    TimePicker myTimePicker; 

private SQLiteConnectornew loginDataBaseAdapter; 


     protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_alertaddlist); 

    Button bt1 = (Button) findViewById(R.id.alerttime); 

     bt1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       openTimePickerDialog(false); 
      }});} 

      private void openTimePickerDialog(boolean is24r){ 
     Calendar calendar = Calendar.getInstance(); 

     timePickerDialog = new TimePickerDialog(
      Alertedit.this, 
      onTimeSetListener, 
      calendar.get(Calendar.HOUR_OF_DAY), 
      calendar.get(Calendar.MINUTE), 
      is24r); 
     timePickerDialog.setTitle("Set Alarm Time"); 

     timePickerDialog.show();} 

     OnTimeSetListener onTimeSetListener 
     = new OnTimeSetListener(){ 

     @Override 
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 

      Calendar calNow = Calendar.getInstance(); 
      Calendar calSet = (Calendar) calNow.clone(); 

      calSet.set(Calendar.HOUR_OF_DAY, hourOfDay); 
      calSet.set(Calendar.MINUTE, minute); 
      calSet.set(Calendar.SECOND, 0); 
      calSet.set(Calendar.MILLISECOND, 0); 

      if(calSet.compareTo(calNow) <= 0){ 
       //Today Set time passed, count to tomorrow 
       calSet.add(Calendar.DATE, 1); 
      } 

      setAlarm(calSet); 
     } 

     private void setAlarm(Calendar targetCal) { 
      // TODO Auto-generated method stub 
      bt1.setText("" + targetCal.getTime()); 
      Log.d("set", "Alarm set"); 
      Intent intent = new Intent(Alertedit.this, AlarmReciever.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(Alertedit.this, RQS_1, intent, 0); 
      AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
      alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); 

     }}; 

2.Receiver類:

public class AlarmReciever extends BroadcastReceiver { 


@Override 
public void onReceive(Context context, Intent intent) { 
    try { 
     /* Display an alert */ 
     Log.e("value", "how are you??"); 
     Toast.makeText(context, "How are you?", Toast.LENGTH_LONG).show(); 
    } 
    catch (Exception r) { 
     Toast.makeText(context, "You were supposed to do something" 
       +" now but I can't retrieve what it was.", 
       Toast.LENGTH_SHORT).show(); 
     Log.e("ALARM_RECEIVER", r.toString()); 
    } 
    } 

}

3.Manifest文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.contactlist" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="7" 
    android:targetSdkVersion="17" /> 
<uses-permission android:name="android.permission.INTERNET"/> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:screenOrientation="portrait" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.contactlist.Primemfrontpage" 

     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
      <activity 
     android:name="com.example.contactlist.Login" 
     android:label="@string/app_name" > 
    </activity> 
      <activity 
     android:name="com.example.contactlist.Registration" 
     android:label="@string/app_name" > 
    </activity> 
    <activity 
     android:name="com.example.contactlistnew.Alertedit" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Translucent.NoTitleBar" > 
    </activity> 

    <receiver android:name="com.example.contactlistnew.AlarmReceiver"> 
    </receiver></application></manifest> 

回答

0

看來你應該爲你的添加意圖過濾器ceiver,如下

<receiver android:name="com.example.contactlistnew.AlarmReceiver"> 
    <intent-filter> 
     <action android:name="android.alarm.demo.action" /> 
    </intent-filter> 
</receiver> 
+0

在我看來的東西,當時間到了報警只發送廣播。 – Jun

+0

也可以設置過去的時間來報警。如果時間過去,報警將立即觸發。 – Jun

0

從廣播接收器必須讀你的意圖的消息,如在

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 

public class MyBroadcastReceiver extends BroadcastReceiver{ 

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

     // Extract data included in the Intent 
     CharSequence intentData = intent.getCharSequenceExtra("message"); 
     Toast.makeText(context, "Received the Intent's message: "+intentData, Toast.LENGTH_LONG).show(); 
    } 

}