报警,并发出通知,通知不会弹出 [英] Alarm with notification, notification wont pop up
问题描述
所以我不知道为什么会这样code不工作。我想提出一个报警通知,将熄灭,每日一次。只是想说,我是新来的机器人。改变了code有点几次,但仍然无法工作。
报警方式通知执行不太多,但我得到这个:-248 /? D / PowerManagerService:releaseWakeLock标志=为0x1 =标签AlarmManager W / ActivityManager:无法启动服务意向{FLG =为0x4 = CMP com.example.polakken.test / .lol(有演员)}:找不到06-13 00:00 :00.825 231-267 /? D / PowerManagerService:acquireWakeLock标志=为0x1 =标签06-13 AlarmManager 00:00:00.825 231-248 /? D / PowerManagerService:releaseWakeLock标志=为0x1 =标签AlarmManager
块引用>我的code:
公共类MainActivity扩展ActionBarActivity {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 共享preferences preferences = preferenceManager.getDefaultShared preferences(本);
共享preferences.Editor编辑器= preferences.edit();
INT I = preferences.getInt(numberoflaunches,1); 如果(ⅰ2){
alarmMethod();
我++;
editor.putInt(numberoflaunches我);
editor.commit();
} 如果(savedInstanceState == NULL){
splashMethod(); }
}// ...私人无效alarmMethod(){
意图intentbro =新意图(这一点,lol.class);
AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE); 的PendingIntent的PendingIntent = PendingIntent.getService(在此,0,intentbro,0); 台历挂历= 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启动报警,Toast.LENGTH_LONG).show(); }//通知类公共类笑延伸活动{ @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); NotificationCompat.Builder B =新NotificationCompat.Builder(本); 意图intent1 =新意图(this.getApplicationContext(),MainActivity.class);
的PendingIntent pIntent = PendingIntent.getActivity(在此,1,intent1,0); b.setContentText(笑);
b.setContentTitle(违约通知);
b.setSmallIcon(R.drawable.iconography_small_size);
b.setContentIntent(pIntent); NotificationManager notificationManager =(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,b.build()); }
}
解决方案您所创建的
的PendingIntent
与PendingIntent.getService()
,但你提供一个意图
与活动
。因此,对于你code工作,你需要使用<一个href=\"http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context,%20int,%20android.content.Intent,%20int)\"相对=nofollow>
PendingIntent.getActivity()
。
(另外,你必须添加相应的&LT;活动方式&gt;
标记你的的AndroidManifest.xml
)不过,这可能不是正是你想要的一切:
哈哈
活动做的唯一事情就是添加一个通知。您可能需要使用广播接收器
(或者一个WakefulBroadcastReceiver
)代替。在这种情况下,你需要使用<一个href=\"http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast(android.content.Context,%20int,%20android.content.Intent,%20int)\"相对=nofollow> PendingIntent.getBroadcast()而不是getActivity()
。So I don't know why this code is not working. I wanted to make an "alarm" notification that would go off once a day. Just wanted to say I'm new to android. Changed up the code a bit couple of times but still wont work.
Alarm method executes notification does too but I get this : -248/? D/PowerManagerService﹕ releaseWakeLock flags=0x1 tag=AlarmManager W/ActivityManager﹕ Unable to start service Intent { flg=0x4 cmp=com.example.polakken.test/.lol (has extras) }: not found 06-13 00:00:00.825 231-267/? D/PowerManagerService﹕ acquireWakeLock flags=0x1 tag=AlarmManager 06-13 00:00:00.825 231-248/? D/PowerManagerService﹕ releaseWakeLock flags=0x1 tag=AlarmManager
My Code:
public class MainActivity extends ActionBarActivity { @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()); } }
解决方案You are creating the
PendingIntent
withPendingIntent.getService()
, but you are supplying anIntent
with anActivity
.So for your code to work you would need to use
PendingIntent.getActivity()
. (Additionally you have to add the corresponding<activity>
tag to yourAndroidManifest.xml
.)However, this is probably not exactly what you want: The only thing the
lol
Activity does is adding a notification. You might want to use aBroadcastReceiver
(or perhaps aWakefulBroadcastReceiver
) instead. In this case you would need to use PendingIntent.getBroadcast() instead ofgetActivity()
.这篇关于报警,并发出通知,通知不会弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!