尝试在特定时间创建通知
我删除了日历属性,它在我运行应用程序后直接运行 但是当我尝试使用日历设置特定时间时,我在设置它时不起作用
MainActivity
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.*;
import android.*;
import android.support.v4.app.NotificationCompat;
import android.content.*;
import android.view.View;
import android.widget.Button;
import java.util.Calendar;
import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
public class MainActivity extends ActionBarActivity {
private PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 48);
calendar.set(Calendar.SECOND, 00);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,
myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
} //end onCreate
}
MyReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
MyAlarmService
mport android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.app.*;
/**
* Created by osamaalammari on 4/10/15.
*/
public class MyAlarmService extends Service {
private NotificationManager mManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
mManager = (NotificationManager)
this.getApplicationContext().getSystemService(this.getApplicationContext().
NOTIFICATION_SERVICE);
Intent intent1 = new
Intent(this.getApplicationContext(),MainActivity.class);
Notification notification = new
Notification(R.drawable.ic_launcher,"This is a test message!",
System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent =
PendingIntent.getActivity( this.getApplicationContext(),0,
intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(),
"AlarmManagerDemo", "This is a test message!",
pendingNotificationIntent);
mManager.notify(0, notification);
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
答案 0 :(得分:0)
尝试这样的事情。
public class MyAlarmService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
CreateNotification(context, "Forgetting something?", "Click here for more details", "Alert");
}
private void CreateNotification(Context context, String title, String text, String alert) {
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
.setTicker(alert)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(text)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
notification.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification.build());
}
}
请记住在清单中添加这样的内容:
<receiver
android:name="yourpackagename.MyAlarmService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
然后在您希望发出通知的班级中:
notificationTime = calendar.getTimeInMillis();
Intent intent = new Intent(getActivity(), MyAlarmService.class);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, notificationTime, PendingIntent.getBroadcast(getActivity()
.getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(getActivity(), "Notification sent!" ,Toast.LENGTH_SHORT).show();