may*_*ysi 27 android android-intent android-notifications intentservice android-activity
当用户按下Send "Button 1"(向下滚动以查看应用程序的构造)时,将Notification创建一个新的RefreshService.如果用户按下此通知,则MainActivity实例启动并接收String具有Button 1超过该值的值Intent.
显示该值.
当用户现在按下时,Send "Button 2"将Notification创建一个新的RefreshService.如果用户按下此通知,则会MainActivity启动一个实例,并接收一个ALSO值超过该值的ALSO.String Button 1Intent
所以你可以猜测,通常应该有价值Button 2.
当用户按下的第一个按钮时,Send "Button 2"总会Button 2发送.
获得第二个按钮值的唯一解决方案是重新启动手机并先按下第二个按钮.即使强行关闭也行不通.
我知道我也可以用另一种方式更改UI.但是我需要在应用程序中使用这种方法,我需要用另一个重新启动'MainActivity',Intent因此方法应该是相同的.
一个Activity叫MainActivity
一个IntentService叫RefreshService
主要活动
public class MainActivity extends Activity implements View.OnClickListener {
public static final String RECEIVED = "received";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView)findViewById(R.id.textView_received)).setText(getIntent().getStringExtra(RECEIVED));
findViewById(R.id.button_1).setOnClickListener(this);
findViewById(R.id.button_2).setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, RefreshService.class);
if(v.getId() == R.id.button_1){
intent.putExtra(RECEIVED, "Button 1");
Toast.makeText(this,"Sent \"Button 1\"",Toast.LENGTH_LONG).show();
}
else if(v.getId() == R.id.button_2){
intent.putExtra(RECEIVED, "Button 2");
Toast.makeText(this,"Sent \"Button 2\"",Toast.LENGTH_LONG).show();
}
startService(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
RefreshService
public class RefreshService extends IntentService {
public RefreshService() {
super("RefreshService");
}
@Override
protected void onHandleIntent(Intent intent) {
String received = intent.getStringExtra(MainActivity.RECEIVED);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra(MainActivity.RECEIVED, received);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle("IntentServiceRefresh").setContentText(received).setSmallIcon(R.drawable.ic_notification_small).setContentIntent(pendingIntent);
Notification notification = builder.build();
// Hide the notification after it's selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
Run Code Online (Sandbox Code Playgroud)
应用布局
sdd*_*ico 79
我的怀疑是,由于Intent中唯一改变的是附加功能,因此PendingIntent.getActivity(...)工厂方法只是将旧意图重新用作优化.
在RefreshService中,尝试:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)
看到:
http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT
| 归档时间: |
|
| 查看次数: |
12680 次 |
| 最近记录: |