我开发了一个应用程序将图像上传到服务器(如Facebook图片上传),我使用后台服务上传图片,上传开始时,会显示通知进度,当用户看到通知时,我想显示一个弹出窗口是否取消或继续上传,所以我在一个活动中写了onNewIntent方法,当用户点击通知时,该活动(Say活动ImageViewActivity)将启动,我想显示弹出窗口。但是onNewIntent方法没有执行而是执行了ImageViewActivity的onCreate方法。为什么会这样???该活动的启动模式是SingleTop.Can任何人都可以帮助我?
MyService.java中的通知和待处理意图
private void startNotification() {
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence notiText = "Your notification from the service";
long meow = System.currentTimeMillis();
Notification notification = new Notification(icon, notiText, meow);
Context context = getApplicationContext();
CharSequence contentTitle = "Your notification";
CharSequence contentText = "Some data has arrived!";
//PENDING INTENT
Intent notificationIntent = new Intent(this, ImageViewActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
int SERVER_DATA_RECEIVED = 1;
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
}
ImageViewActivity.java中的onNewIntent方法
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
initiatePopupWindow();
}
这里我调用的是initiatePopupWindow()方法。
public void initiatePopupWindow() {
try {
LayoutInflater inflater;
inflater = (LayoutInflater) ImageViewActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popupmenu, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
// pwindo.setFocusable(true);
pwindo.setOutsideTouchable(true);
// pwindo.showAsDropDown();
Button btnStopUpload = (Button) layout.findViewById(R.id.btn_upload);
btnStopUpload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// mNotifyManager.cancel(MY_NOTIFICATION_ID);
// mTask.cancel(true);
Log.e(TAG, "Notification Cancelled ");
// mTask.cancel(true);
/* Toast.makeText(getApplicationContext(),
"Upload Cancelled", Toast.LENGTH_SHORT).show();*/
Intent i=new Intent(ImageViewActivity.this,MyService.class);
//stopService(i);
ImageViewActivity.this.stopService(i);
ImageViewActivity.this.finish();
}
});
Button btnCancelPopup = (Button) layout.findViewById(R.id.btn_cancel);
btnCancelPopup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"POPUP Rejected", Toast.LENGTH_SHORT).show();
Log.e("COOKBOOK", "I got an error", e);
e.printStackTrace();
}
}
logcat的
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference
at project1.me.com.cookbookintent.ImageViewActivity.initiatePopupWindow(ImageViewActivity.java:266)
at project1.me.com.cookbookintent.ImageViewActivity.onCreate(ImageViewActivity.java:90)
at android.app.Activity.performCreate(Activity.java:5953)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
OnCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
Log.i(TAG, "Activity created");
try{
Intent intent = getIntent();
if (intent.getBooleanExtra("FROM_NOTIFICATION",false)) {
Toast.makeText(getApplicationContext(),
"Reject", Toast.LENGTH_SHORT).show();
initiatePopupWindow();
}
}
// int value= intent.getExtras().getInt("FROM_NOTIFICATION");
// if(value==2){
catch (Exception e){
Log.i(TAG, "Rejected");
Toast.makeText(getApplicationContext(),
"Reject", Toast.LENGTH_SHORT).show();
}
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
//Image from sdcard
try {
Intent intent1 = getIntent();
picturePath = intent1.getExtras().getString("key");
state = intent1.getExtras().getInt("first_state");
imageView = (ImageView) findViewById(R.id.imgView);
decodeFile(picturePath);
Toast.makeText(getApplicationContext(),
"Gallery State" + picturePath, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"pic not found", Toast.LENGTH_SHORT).show();
}
///Camera Image
try {
if (picturePath == null) {
Intent camIntent = getIntent();
picturePath= camIntent.getExtras().getString("filePath");
state = camIntent.getExtras().getInt("first_state");
imageView = (ImageView) findViewById(R.id.imgView);
decodeFile(picturePath);
Toast.makeText(getApplicationContext(), "PATH" + picturePath, Toast.LENGTH_SHORT).show();
bitmap = ShrinkBitmap(picturePath, 300, 300);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Please select image", Toast.LENGTH_SHORT).show();
}
uploadPicture();
}
答案 0 :(得分:0)
这正是预期的结果:如果Activity没有被销毁,你在onNewIntent中获得Intent,否则将创建一个新的Activity,你必须处理onCreate中的Intent。一个简单的代码片段,用于创建PopupWindow:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View root = findViewById(R.id.parentView);
root.post(new Runnable() {
@Override
public void run() {
LayoutInflater inflater = (LayoutInflater)
MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popup_view, null, false);
PopupWindow popupWindow = new PopupWindow(view, 300, 300, true);
popupWindow.showAtLocation(findViewById(R.id.test), Gravity.CENTER, 0, 0);
}
});
}
其中R.id.parentView是您的活动视图之一,而R.layout.popup_view是您的PopupWindow布局。
答案 1 :(得分:0)
这里接受的答案并非如此。如果活动已在任务列表中,则将调用onNewIntent。无论它是否被摧毁。
因此,在内存不足的情况下,你会看到onCreate然后onNewIntent被调用。
如果你完成了()一项活动,那么它就不再在你的任务列表中,或者如果活动刚刚被添加到任务列表中,那么onNewIntent就不会被调用。