2016-02-13 98 views
0

我想在應用程序中設置小時和分鐘時啓動鬧鐘管理器,但在我的代碼中鬧鐘未設置開始。報警管理器沒有發出報警?

我不確定它是否與我的設備時間匹配,或者如果我的代碼有任何其他問題。任何幫助將不勝感激。

這裏是我的代碼:

public class Alarmactivity extends Activity { 

AlarmManager alarmManager; 
private PendingIntent pendingIntent; 
private TimePicker alarmTimePicker; 
private static Alarmactivity inst; 
private TextView alarmTextView; 

public static Alarmactivity instance() { 
    return inst; 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    inst = this; 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 
    alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker); 
    alarmTextView = (TextView) findViewById(R.id.alarmText); 
    ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle); 
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
} 

public void onToggleClicked(View view) { 
    if (((ToggleButton) view).isChecked()) { 
     Log.d("MyActivity", "Alarm On"); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour()); 
     calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute()); 
     Intent myIntent = new Intent(Alarmactivity.this, AlarmReceiver.class); 
     pendingIntent = PendingIntent.getBroadcast(Alarmactivity.this, 0, myIntent, pendingIntent.FLAG_ONE_SHOT); 
     alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent); 
    } else { 
     alarmManager.cancel(pendingIntent); 
     setAlarmText(""); 
     Log.d("MyActivity", "Alarm Off"); 
    } 
} 

public void setAlarmText(String alarmText) { 
    alarmTextView.setText(alarmText); 
}} 

AlarmReceiver.java

public class AlarmReceiver extends WakefulBroadcastReceiver { 

@Override 
public void onReceive(final Context context, Intent intent) { 
    //this will update the UI with message 
    Alarmactivity inst = Alarmactivity.instance(); 
    inst.setAlarmText("Alarm! Wake up! Wake up!"); 

    //this will sound the alarm tone 
    //this will sound the alarm once, if you wish to 
    //raise alarm in loop continuously then use MediaPlayer and setLooping(true) 
    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
    if (alarmUri == null) { 
     alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    } 
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri); 
    ringtone.play(); 

    //this will send a notification message 
    ComponentName comp = new ComponentName(context.getPackageName(), 
      AlarmService.class.getName()); 
    startWakefulService(context, (intent.setComponent(comp))); 
    setResultCode(Activity.RESULT_OK); 
}} 

AlarmService.java

public class AlarmService extends IntentService { 
private NotificationManager alarmNotificationManager; 

public AlarmService() { 
    super("AlarmService"); 
} 

@Override 
public void onHandleIntent(Intent intent) { 
    sendNotification("Wake Up! Wake Up!"); 
} 

private void sendNotification(String msg) { 
    Log.d("AlarmService", "Preparing to send notification...: " + msg); 
    alarmNotificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, Alarmactivity.class), 0); 

    NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
      this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
      .setContentText(msg); 


    alamNotificationBuilder.setContentIntent(contentIntent); 
    alarmNotificationManager.notify(1, alamNotificationBuilder.build()); 
    Log.d("AlarmService", "Notification sent."); 
}} 

的AndroidManifest.xml

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="21" /> 

<uses-permission android:name="android.permission.WAKE_LOCK" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".Alarmactivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service 
     android:name=".AlarmService" 
     android:enabled="true" /> 

    <receiver android:name=".AlarmReceiver" /> 
</application> 
+0

您是否在指定的鬧鐘時間收到通知或烤麪包? – OBX

+0

我希望在指定的時間內通過鬧鐘鈴聲進行通知。 – hamed

+0

我在問,目前您是否在設定的時間收到通知? – OBX

回答

0

試試這個,它的工作對我來說:

MainActivity.class

public class MainActivity extends AppCompatActivity 
{ 
    AlarmManager alarmManager; 
    private PendingIntent pendingIntent; 
    private TimePicker alarmTimePicker; 
    private static MainActivity inst; 
    private TextView alarmTextView; 

    public static MainActivity instance() { 
     return inst; 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     inst = this; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker); 
     alarmTextView = (TextView) findViewById(R.id.alarmText); 
     ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle); 
     alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    } 

    public void onToggleClicked(View view) { 
     if (((ToggleButton) view).isChecked()) { 
      Log.d("MyActivity", "Alarm On"); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour()); 
      calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute()); 
      Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class); 
      pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0); 
      alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent); 
     } else { 
      alarmManager.cancel(pendingIntent); 
      // setAlarmText(""); 
      Log.d("MyActivity", "Alarm Off"); 
     } 
    } 
} 

AlarmReceiver.class

public class AlarmReceiver extends WakefulBroadcastReceiver 
{ 
    @Override 
    public void onReceive(final Context context, Intent intent) { 
     //this will update the UI with message 
     MainActivity inst = MainActivity.instance(); 
     //inst.setAlarmText("Alarm! Wake up! Wake up!"); 

     //this will sound the alarm tone 
     //this will sound the alarm once, if you wish to 
     //raise alarm in loop continuously then use MediaPlayer and setLooping(true) 
     Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
     if (alarmUri == null) { 
      alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     } 
     Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri); 
     ringtone.play(); 

     //this will send a notification message 
     ComponentName comp = new ComponentName(context.getPackageName(), 
       AlarmService.class.getName()); 
     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 

AlarmService.class

public class AlarmService extends IntentService 
{ 
    private NotificationManager alarmNotificationManager; 

    public AlarmService() { 
     super("AlarmService"); 
    } 

    @Override 
    public void onHandleIntent(Intent intent) { 
     sendNotification("Wake Up! Wake Up!"); 
    } 

    private void sendNotification(String msg) { 
     Log.d("AlarmService", "Preparing to send notification...: " + msg); 
     alarmNotificationManager = (NotificationManager) this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, MainActivity.class), 0); 

     NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     Notification myNotication; 

     Intent intent = new Intent("com.rj.notitfications.SECACTIVITY"); 

     PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0); 

     Notification.Builder builder = new Notification.Builder(getApplicationContext()); 

     builder.setAutoCancel(false); 
     //builder.setTicker("this is ticker text"); 
     builder.setContentTitle("Alarm Notification"); 
     builder.setContentText("Alarm Playing"); 
     builder.setSmallIcon(R.drawable.mail2); 
     builder.setContentIntent(pendingIntent); 
     builder.setOngoing(true); 
     builder.setSubText("This is subtext..."); //API level 16 
     builder.setNumber(100); 
     builder.build(); 

     myNotication = builder.getNotification(); 
     manager.notify(11, myNotication); 

    } 
}