所以我不知道为什么这段代码不起作用。我想做一个"闹钟"通知每天会发生一次。只是想说我是android的新手。更改了代码几次但仍无法正常工作。
警报方法也会执行通知,但我得到了这个:-248 /? D / PowerManagerService:releaseWakeLock flags = 0x1 tag = AlarmManager W / ActivityManager:无法启动服务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
我的代码:
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());
}
}
答案 0 :(得分:0)
您正在使用PendingIntent
创建PendingIntent.getService()
,但是您正在提供Intent
Activity
。
因此,要使代码正常工作,您需要使用PendingIntent.getActivity()
。
(此外,您必须将相应的<activity>
标记添加到AndroidManifest.xml
。)
但是,这可能不是您想要的:lol
Activity唯一做的就是添加通知。您可能希望使用BroadcastReceiver
(或者可能是WakefulBroadcastReceiver
)。在这种情况下,您需要使用PendingIntent.getBroadcast()而不是getActivity()
。
答案 1 :(得分:0)
请按照以下步骤操作
1创建一个Activity并将此代码添加到setAlarm(可以是任何名称)
假设您要为24/02 / 15,14:00 PM设置闹钟
@SuppressLint("UseSparseArrays")
public void setAlarms(Long id,String event_date,String event_time,Context context)
{
Calendar cal=Calendar.getInstance();
String[] parts=event_date.split("/");
String Event_date=parts[0];
String Event_month=parts[1];
String Event_Year=parts[2];
String[] parts1=event_time.split(":");
String Event_HOD=parts1[0];
String Event_MIN=parts1[1];
cal.add(Calendar.YEAR, Integer.parseInt(Event_Year)-cal.get(Calendar.YEAR));
cal.add(Calendar.MONTH, (Integer.parseInt(Event_month)-1)-cal.get(Calendar.MONTH));
cal.add(Calendar.DAY_OF_MONTH, Integer.parseInt(Event_date)-cal.get(Calendar.DAY_OF_MONTH));
cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(Event_HOD)-cal.get(Calendar.HOUR_OF_DAY));
cal.add(Calendar.MINUTE, Integer.parseInt(Event_MIN)-cal.get(Calendar.MINUTE));
cal.add(Calendar.SECOND, 0);
AlarmManager am =(AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
//Your BroadcastReceiver which will receive Alarm
Intent intent = new Intent(context, AlarmReceiver.class);
//you can use PutExtra() to send additional parameter with Intent
//PendingIntent to add
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
(int)(long)id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent);
Toast.makeText(context, "alarm Set for Know", Toast.LENGTH_LONG).show();
}
2)内部BroadcastReceiver onReceive()方法
@Override
public void onReceive(Context context, Intent intent) {
//Receive parameter and do what you want to do with parameter
//Using RemoteViews you can Inflate a CustomLayout when user clicks on Notification when you do not want any CustomLayout for Notification remove it
RemoteViews contentView1 = new RemoteViews("com.example.new_reminder_book" , R.layout.custom_notification);
contentView1.setTextViewText(R.id.cn_tv_title, Event_title);
contentView1.setTextViewText(R.id.cn_tv_category, Event_category);
contentView1.setImageViewResource(R.id.cn_tv_image, R.drawable.event);
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.appicon)
.setContent(contentView1)
.setAutoCancel(true)
.setOngoing(true);
//Target Activity that will start when you click on Notification
Intent result=new Intent(context, EmptyActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(EmptyActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(result);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification=mBuilder.build();
notification.contentView=contentView1;
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify((int) System.currentTimeMillis(), notification);
//A mp3 fine in res->raw that will play when notification fire
MediaPlayer mp = MediaPlayer.create(context, R.raw.listen);
mp.start();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
/*Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
//Set pattern for Vibration
long[] pattern={2000,3000,4000};
vibrator.vibrate(pattern,1);*/
wl.release();
}
}
3)在Manifest.xml中声明两个权限
<uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.VIBRATE"/>
4)在Manifest.xml中声明你的AlarmReceiver
<receiver android:name=".extra.AlarmReceiver" android:label="@string/title_activity_alarm_receiver" android:exported="true"> </receiver>
请遵循这些文件
http://developer.android.com/reference/android/content/BroadcastReceiver.html
http://developer.android.com/reference/android/app/AlarmManager.html
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
http://developer.android.com/reference/android/os/PowerManager.WakeLock.html
希望这会对你有所帮助