package
com
.eboy.testnotification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android. net .Uri;
import android.os.Bundle;
import android.v ie w.Menu;
import android.view.View;
public class MainActivity extends Activity {
private static final String edtShortTitle = "edtShortTitle";
private static final String edtTitle = "edtTitle";
private static final String edtContent = "http://www.985dy.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendNotification1(View v){
int icon = android.R.drawable.stat_notify_error;
Notification notification = new Notification(icon, edtShortTitle, System.currentTimeMillis());
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(edtContent));
PendingIntent pendingIntent = PendingIntent.getActivity( this , 0, intent, 0);
notification.setLatestEventInfo( this , edtTitle, edtContent, pendingIntent);
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_AUTO_CANCEL; // 不可清除,自动取消
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(100, notification);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true ;
}
}
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android. net .Uri;
import android.os.Bundle;
import android.v ie w.Menu;
import android.view.View;
public class MainActivity extends Activity {
private static final String edtShortTitle = "edtShortTitle";
private static final String edtTitle = "edtTitle";
private static final String edtContent = "http://www.985dy.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendNotification1(View v){
int icon = android.R.drawable.stat_notify_error;
Notification notification = new Notification(icon, edtShortTitle, System.currentTimeMillis());
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(edtContent));
PendingIntent pendingIntent = PendingIntent.getActivity( this , 0, intent, 0);
notification.setLatestEventInfo( this , edtTitle, edtContent, pendingIntent);
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_AUTO_CANCEL; // 不可清除,自动取消
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(100, notification);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true ;
}
}
本文详细介绍了如何在Android应用中发送自定义通知并创建菜单项,包括通知的图标、标题、内容以及如何实现自动取消和不移除功能。
1888

被折叠的 条评论
为什么被折叠?



