1

我是剛剛接觸android並致力於通知鬧鐘管理器顯示通知。我在API 23上。 我在下面發佈的代碼完美運行,但我無法提供它爲什麼沒有通知我?未顯示通知的鬧鐘管理器

Can任何人都可以幫助我什麼是它的錯誤? 謝謝。

MainActivity

public class MainActivity extends AppCompatActivity { 

Button b; 
private GoogleApiClient client; 

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

    b = (Button) this.findViewById(R.id.btn); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 


    Calendar calendar = Calendar.getInstance(); 

    calendar.set(Calendar.MONTH, 11); 
    calendar.set(Calendar.YEAR, 2015); 
    calendar.set(Calendar.DAY_OF_MONTH, 30); 

    calendar.set(Calendar.HOUR_OF_DAY, 15); 
    calendar.set(Calendar.MINUTE, 34); 
    calendar.set(Calendar.SECOND, 0); 
    calendar.set(Calendar.AM_PM,Calendar.PM); 

    Intent myIntent = new Intent(MainActivity.this, AlarmBroadcastReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent); 

} 

AlarmBroadcastReceiver

public class AlarmBroadcastReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    Intent startIntent = new Intent(context, AlarmService.class); 
    context.startService(startIntent); 
} 
} 

AlarmService

public class AlarmService extends Service{ 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    generarNotificacion(); 

    return Service.START_NOT_STICKY; 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

public void generarNotificacion(){ 

    Intent resultIntent = new Intent(this.getApplicationContext(), MainActivity.class); 

    android.support.v4.app.NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.heart1) 
        .setContentTitle(getResources().getString(R.string.app_name)) 
        .setContentText(getResources().getString(R.string.text)); 

    PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
        this, 
        0, 
        resultIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 

    mBuilder.setContentIntent(resultPendingIntent); 

    // Sets an ID for the notification 
    int mNotificationId = 001; 

    // Gets an instance of the NotificationManager service 
    NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    // Builds the notification and issues it. 
    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
} 

} 

Android清單

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

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

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

幾個月是從零開始的。如果你想要十一月,相應的值是'10'。更好的選擇是使用類常量:'Calendar.NOVEMBER'。 –

+1

@MikeM。這正好解決了我的問題。謝謝。 – Abhishek

+0

@MikeM。你能幫助我嗎?我如何在每個半小時內爲每個特定的一天設置此通知,例如12月1日,我的通知將在每半小時內顯示出來? – Abhishek

回答

0

看樣子,你要爲如今的報警,不久的未來。但是,月份基於零,所以11的值實際上是12月份。如果您想要11月份,則相應的值爲10。不過,更好的選擇是使用類常量:Calendar.NOVEMBER