2015-02-23 46 views
-1

所以我不知道爲什麼這段代碼不工作。我想發出一個「警報」通知,每天一次。只是想說,即時通訊新的android。謝謝。代碼無法正常工作。通知應該每天關閉一次

編輯:改變了一下代碼。報警方法執行和通知也一樣,但我得到這個錯誤信息:

-248 /? D/PowerManagerService:releaseWakeLock標誌= 0x1 tag = AlarmManager W/ActivityManager:無法啓動服務意圖{flg = 0x4 cmp = com.example.polakken.test/.lol(有額外)}:未找到06-13 00:00 :00.825 231-267 /? D/PowerManagerService:acquireWakeLock標誌= 0x1標籤= AlarmManager 06-13 00:00:00.825 231-248 /? d/PowerManagerService:releaseWakeLock標誌=爲0x1 =標籤AlarmManager -

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

     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
     SharedPreferences.Editor editor = preferences.edit(); 
     int i = preferences.getInt("numberoflaunches", 1); 

     if (i < 2) { 
      alarmMethod(); 
      i++; 
      editor.putInt("numberoflaunches", i); 
      editor.commit(); 
     } 

     if (savedInstanceState == null) { 
      splashMethod(); 

     } 




    } 

//... 

private void alarmMethod() { 
     Intent intentbro = new Intent(this, lol.class); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

     PendingIntent pendingIntent = PendingIntent.getService(this, 0, intentbro, 0); 

     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.SECOND, 0); 
     calendar.set(Calendar.MINUTE, 0); 
     calendar.set(Calendar.HOUR, 0); 
     calendar.set(Calendar.AM_PM, Calendar.AM); 
     calendar.add(Calendar.DAY_OF_MONTH, 1); 

     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent); 


     Toast.makeText(MainActivity.this, "start alarm", Toast.LENGTH_LONG).show(); 


    } 

//notification class 

public class lol extends Activity{ 

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


     NotificationCompat.Builder b = new NotificationCompat.Builder(this); 


     Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent1, 0); 


     b.setContentText("lol"); 
     b.setContentTitle("Default notification"); 
     b.setSmallIcon(R.drawable.iconography_small_size); 
     b.setContentIntent(pIntent); 







     NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(1, b.build()); 




    } 
    } 
+1

如果發生什麼事情,你究竟發生了什麼?你有錯誤嗎?如果是這樣,發佈它們。 – JNYRanger 2015-02-23 19:46:44

+0

@JNYRanger問題是沒有錯誤的。應用程序運行完美但無法按代碼中列出的時間顯示通知 – 2015-02-23 19:53:28

+1

您應該測試即將到來的日曆時間,並在代碼中設置斷點。查看您的斷點是否執行,並告訴我們您的代碼的哪一部分未按預期工作。 – 2015-02-23 19:57:41

回答

0

你加入一個額外的一天,你的報警。它應該是這樣的:

... 
Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.SECOND, 0); 
     calendar.set(Calendar.MINUTE, 0); 
     calendar.set(Calendar.HOUR_OF_DAY, 1); 
     calendar.set(Calendar.AM_PM, Calendar.AM); 

     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
... 
+0

仍然沒有運行.. – 2015-02-23 20:17:58

+0

那麼上面的代碼是安排通知凌晨1點。直到凌晨1點纔會熄滅! – C0D3LIC1OU5 2015-02-23 20:26:11

+0

另外,我假設您的BroadcastReciever設置正確。請參閱本教程,特別是BorodcastReceiver部分。 HTTP:// javatechig。com/android/repeat-alarm-example-in-android – C0D3LIC1OU5 2015-02-23 20:32:39

0

要調用PendingIntent pendingIntent = PendingIntent.getService(this, 0, intentbro, 0);

但是,你需要在你AndroidManifest宣佈這個服務是這樣的:

<service android:name=".Lol" /> 

此外,在Java類應該是大寫,但您的意圖調用小寫類:

Intent intentbro = new Intent(this, lol.class); 

我建議你對你的代碼運行靜態分析,看看如何改進它!