如何将变量从notificationmanager传递到android中的活动
我打算在notification manager中设置一个变量,这是我第一次尝试成功,但第二次尝试新消息时,活动显示了thr perivous value plz help me我确实遇到了一个大问题如何将变量从notificationmanager传递到android中的活动,android,notificationmanager,Android,Notificationmanager,我打算在notification manager中设置一个变量,这是我第一次尝试成功,但第二次尝试新消息时,活动显示了thr perivous value plz help me我确实遇到了一个大问题 这是通知管理器的代码 在receivemessage类中 提前感谢在第#1节中,您正在添加此值: notificationIntent.putExtra("activate",message.toString()); 在receivemessage类中(顺便说一句,命名错误,类名应该是驼峰大小写
notificationIntent.putExtra("activate",message.toString());
在receivemessage类中(顺便说一句,命名错误,类名应该是驼峰大小写),您有:
Intent i=getIntent();
saveit = i.getStringExtra("run");
也许你应该在那里:
saveit = i.getStringExtra("activate");
这个想法是,从您发布的内容来看,不清楚是否有任何组件实际提供了这个run
intent字符串
使用您的代码编辑,以便从活动中触发上述通知管理器:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_some_action).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setupClick();
}
});
}
private void setupClick() {
String message = "Sample notification";
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = getString(R.string.app_name);
Intent notificationIntent = new Intent(this, MySoActivity.class);
// set intent so it does not start a new activity
notificationIntent.putExtra("run", message.toString());
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent intent = PendingIntent.getActivity(this.getApplicationContext(), 0,
notificationIntent, 0);
notification.setLatestEventInfo(this.getApplicationContext(), title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
结果是一个MySoActivity
类:
public class MySoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_so_layout);
TextView lblIntentExtra = (TextView) findViewById(R.id.lblIntentExtra);
Intent intent = getIntent();
String value = intent.getStringExtra("run");
if (TextUtils.isEmpty(value)) {
value = "NONE@!";
}
lblIntentExtra.setText(value);
}
}
通知是正确的,当点击通知时,我得到了预期的值。代码中唯一的区别是我使用的是getApplicationContext()
,而不是上面的上下文,但我不确定这有多重要。也许您可以比较这些差异,看看哪里做错了…在第1节中,您添加了以下值:
notificationIntent.putExtra("activate",message.toString());
在receivemessage类中(顺便说一句,命名错误,类名应该是驼峰大小写),您有:
Intent i=getIntent();
saveit = i.getStringExtra("run");
也许你应该在那里:
saveit = i.getStringExtra("activate");
这个想法是,从您发布的内容来看,不清楚是否有任何组件实际提供了这个run
intent字符串
使用您的代码编辑,以便从活动中触发上述通知管理器:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_some_action).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setupClick();
}
});
}
private void setupClick() {
String message = "Sample notification";
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = getString(R.string.app_name);
Intent notificationIntent = new Intent(this, MySoActivity.class);
// set intent so it does not start a new activity
notificationIntent.putExtra("run", message.toString());
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent intent = PendingIntent.getActivity(this.getApplicationContext(), 0,
notificationIntent, 0);
notification.setLatestEventInfo(this.getApplicationContext(), title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
结果是一个MySoActivity
类:
public class MySoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_so_layout);
TextView lblIntentExtra = (TextView) findViewById(R.id.lblIntentExtra);
Intent intent = getIntent();
String value = intent.getStringExtra("run");
if (TextUtils.isEmpty(value)) {
value = "NONE@!";
}
lblIntentExtra.setText(value);
}
}
通知是正确的,当点击通知时,我得到了预期的值。代码中唯一的区别是我使用的是getApplicationContext()
,而不是上面的上下文,但我不确定这有多重要。也许你可以比较一下差异,看看哪里做错了…替换这一行
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent,0);
与
更换这条线
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent,0);
与
在generatenotification()方法中,请使用以下代码:
private void generateNotification(Context context, String message, String query) {
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
Intent intent = new Intent(context, myActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(when)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();
notificationManager.notify((int) when, notification);
}
请参阅:notificationManager.notify((int)when,notification)强>
不要使用0
希望这有帮助。在generatenotification()方法中,请使用以下代码:
private void generateNotification(Context context, String message, String query) {
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
Intent intent = new Intent(context, myActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(when)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();
notificationManager.notify((int) when, notification);
}
请参阅:notificationManager.notify((int)when,notification)强>
不要使用0
希望这有帮助。这段代码对我有用,
试试这个
替换此代码:
在gcminentservice.java中,替换此“generateNotification”方法
gcminentservice.java
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, NotificationReceiver.class);
notificationIntent.putExtra("Notice", message);
notificationIntent.setAction(Intent.ACTION_VIEW);
notificationIntent.setAction("myString"+when);
notificationIntent.setData((Uri.parse("mystring"+when)));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, (int) when, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify((int) when, notification);
}
TextView textshow;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
textshow=(TextView)findViewById(R.id.showmessage);
Intent in = getIntent();
String text = in.getStringExtra("Notice");
textshow.setText(text);
}
Receivemessage.java
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, NotificationReceiver.class);
notificationIntent.putExtra("Notice", message);
notificationIntent.setAction(Intent.ACTION_VIEW);
notificationIntent.setAction("myString"+when);
notificationIntent.setData((Uri.parse("mystring"+when)));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, (int) when, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify((int) when, notification);
}
TextView textshow;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
textshow=(TextView)findViewById(R.id.showmessage);
Intent in = getIntent();
String text = in.getStringExtra("Notice");
textshow.setText(text);
}
这个代码对我有用,
试试这个
替换此代码:
在gcminentservice.java中,替换此“generateNotification”方法
gcminentservice.java
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, NotificationReceiver.class);
notificationIntent.putExtra("Notice", message);
notificationIntent.setAction(Intent.ACTION_VIEW);
notificationIntent.setAction("myString"+when);
notificationIntent.setData((Uri.parse("mystring"+when)));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, (int) when, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify((int) when, notification);
}
TextView textshow;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
textshow=(TextView)findViewById(R.id.showmessage);
Intent in = getIntent();
String text = in.getStringExtra("Notice");
textshow.setText(text);
}
Receivemessage.java
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, NotificationReceiver.class);
notificationIntent.putExtra("Notice", message);
notificationIntent.setAction(Intent.ACTION_VIEW);
notificationIntent.setAction("myString"+when);
notificationIntent.setData((Uri.parse("mystring"+when)));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, (int) when, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify((int) when, notification);
}
TextView textshow;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
textshow=(TextView)findViewById(R.id.showmessage);
Intent in = getIntent();
String text = in.getStringExtra("Notice");
textshow.setText(text);
}
我的缺点是,我已经纠正了这个问题,但它仍然给了我相同的响应。我的老兄,它不起作用,因为它是一个GCMBaseIntentService,它不允许我使用getapplicationcontext,它从
服务扩展到IntentService
,您可以访问getapplicationcontext()
my bad我已经纠正了这个问题,但它仍然给了我相同的响应。我的兄弟,它不起作用,因为它是一个GCMBaseContentService,它不能让我使用getapplicationcontext,它从服务
扩展到意向服务
,你可以访问getapplicationcontext()
(int)(Math.random())*100)是关键。为我工作!(int)(Math.random()*100)是关键。为我工作!