Intent.setClass 在 android
Intent.setClass in android
我正在开发 android 应用程序,我在其中以编程方式创建通知,如下所示:
public void createNotification()
{
Intent intent=new Intent().setClass(IncomingCallScreen.this,IncomingCallScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(IncomingCallScreen.this, 0, intent,0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Incoming call")
.setContentText("there is a call running")
.setSmallIcon(R.drawable.answer)
.setAutoCancel(true)
.addAction(R.drawable.reject, "HangUp", contentIntent)
.setContentIntent(contentIntent)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
这个函数在onpause状态下被调用我想做的是当activity处于onpause并且用户点击通知时activity可以像whatsapp一样再次出现在我接受的时候一个电话,如果我返回或按主页,则会显示一条通知以再次显示呼叫屏幕。
但是当我按下通知时,应用程序关闭了!
注意:上下文与我想去的 class 相同(可能是这个问题)也存在 lunchMode:SinleTop 这个 activity
试试这个,
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, Second.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// notification.defaults |= Notification.DEFAULT_LIGHTS;
//
// notification.ledARGB = 0xff00ff00;
// notification.ledOnMS = 300;
// notification.ledOffMS = 1000;
// notification.defaults |= Notification.FLAG_SHOW_LIGHTS;
// Vibrate if vibrate is enabled
// notification.defaults |= Notification.DEFAULT_VIBRATE;
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.e("screen on.................................", "" + isScreenOn);
if (isScreenOn == false)
{
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyLock");
wl.acquire(10000);
WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyCpuLock");
wl_cpu.acquire(10000);
}
notificationManager.notify(0, notification);
}
从 0 变为 PendingIntent.FLAG_CANCEL_CURRENT
PendingIntent contentIntent = PendingIntent.getActivity(IncomingCallScreen.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
解决了问题
public void createNotification()
{
Intent intent=new Intent(IncomingCallScreen.this,IncomingCallScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(IncomingCallScreen.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new NotificationCompat.Builder(IncomingCallScreen.this)
.setContentTitle("Incoming call")
.setContentText("there is a call running")
.setSmallIcon(R.drawable.answer)
.setAutoCancel(true)
.addAction(R.drawable.reject, "HangUp", contentIntent)
.setContentIntent(contentIntent)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
我正在开发 android 应用程序,我在其中以编程方式创建通知,如下所示:
public void createNotification()
{
Intent intent=new Intent().setClass(IncomingCallScreen.this,IncomingCallScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(IncomingCallScreen.this, 0, intent,0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Incoming call")
.setContentText("there is a call running")
.setSmallIcon(R.drawable.answer)
.setAutoCancel(true)
.addAction(R.drawable.reject, "HangUp", contentIntent)
.setContentIntent(contentIntent)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
这个函数在onpause状态下被调用我想做的是当activity处于onpause并且用户点击通知时activity可以像whatsapp一样再次出现在我接受的时候一个电话,如果我返回或按主页,则会显示一条通知以再次显示呼叫屏幕。 但是当我按下通知时,应用程序关闭了!
注意:上下文与我想去的 class 相同(可能是这个问题)也存在 lunchMode:SinleTop 这个 activity
试试这个,
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, Second.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// notification.defaults |= Notification.DEFAULT_LIGHTS;
//
// notification.ledARGB = 0xff00ff00;
// notification.ledOnMS = 300;
// notification.ledOffMS = 1000;
// notification.defaults |= Notification.FLAG_SHOW_LIGHTS;
// Vibrate if vibrate is enabled
// notification.defaults |= Notification.DEFAULT_VIBRATE;
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.e("screen on.................................", "" + isScreenOn);
if (isScreenOn == false)
{
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyLock");
wl.acquire(10000);
WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyCpuLock");
wl_cpu.acquire(10000);
}
notificationManager.notify(0, notification);
}
从 0 变为 PendingIntent.FLAG_CANCEL_CURRENT
PendingIntent contentIntent = PendingIntent.getActivity(IncomingCallScreen.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
解决了问题
public void createNotification()
{
Intent intent=new Intent(IncomingCallScreen.this,IncomingCallScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(IncomingCallScreen.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new NotificationCompat.Builder(IncomingCallScreen.this)
.setContentTitle("Incoming call")
.setContentText("there is a call running")
.setSmallIcon(R.drawable.answer)
.setAutoCancel(true)
.addAction(R.drawable.reject, "HangUp", contentIntent)
.setContentIntent(contentIntent)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}