如何从OnActivityResult
MainActivity
(从创建它的位置)获取待处理意图的结果?我一直试图使用creatependingresult(...)
但没有取得多大成功
使用的方法基于this问题。
我在运行时的onDestroy函数中遇到nullpointerException
/*code snippet from MainActivity.java*/
public void processRoute() {
Intent proximityIntent = new Intent(getApplicationContext(), ProximityActivity.class);
getresult = this.createPendingResult(counter, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT);
Log.d("get result", "done");
Parcel p = Parcel.obtain();
p.writeValue(getresult);
parcelsend = MyParcel.CREATOR.createFromParcel(p);
proximityIntent.putExtra("attachedIntentParcelable", parcelsend);
proximityIntent.putExtra("counter", counter);
Log.d("notif", "intent attached as parcelable");
pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, proximityIntent, PendingIntent.FLAG_CANCEL_CURRENT);
locationManager.addProximityAlert(orderedList.x.get(counter).x.latitude, orderedList.x.get(counter).x.longitude, 20, -1, pendingIntent);
Log.d("alert added", "proximity alert added");
}
/*ProximityActivity.java*/
package anmol.csp5;
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.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import android.preference.PreferenceManager.OnActivityDestroyListener;
public class ProximityActivity extends Activity {
String notificationTitle;
String notificationContent;
String tickerMessage;
PendingIntent receive;
Intent msg;
int c;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
boolean proximity_entering = getIntent().getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
MyParcel receivedParcelable = getIntent().getParcelableExtra("attachedIntentParcelable");
c=getIntent().getIntExtra("counter",-1);
Parcel rec = Parcel.obtain();
receivedParcelable.writeToParcel(rec,0);
receive =(PendingIntent) rec.readValue(PendingIntent.class.getClassLoader());
Log.d("notif", "pending intent unpacked");
if(proximity_entering){
Toast.makeText(getBaseContext(),"Entering the region" ,Toast.LENGTH_LONG).show();
notificationTitle="Proximity - Entry";
notificationContent="Entered the region";
tickerMessage = "Entered the region";
Log.d("alert point","entered");
}else{
Toast.makeText(getBaseContext(),"Exiting the region" ,Toast.LENGTH_LONG).show();
notificationTitle="Proximity - Exit";
notificationContent="Exited the region";
tickerMessage = "Exited the region";
Log.d("alert point","exited");
}
msg = new Intent(getApplicationContext(),MainActivity.class);
msg.putExtra("entered",proximity_entering);
// receivedIntent = PendingIntent.getActivity(getApplicationContext(),count,send,PendingIntent.FLAG_CANCEL_CURRENT);
// try{
// receive.send(c);
// }
// catch (PendingIntent.CanceledException e) {
// Log.d("exception","cancelled");
// e.printStackTrace();
// }
Intent notificationIntent = new Intent(getApplicationContext(),NotificationView.class);
notificationIntent.putExtra("content", notificationContent );
/** This is needed to make this intent different from its previous intents */
notificationIntent.setData(Uri.parse("tel:/"+ (int)System.currentTimeMillis()));
/** Creating different tasks for each notification. See the flag Intent.FLAG_ACTIVITY_NEW_TASK */
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
/** Getting the System service NotificationManager */
NotificationManager nManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
/** Configuring notification builder to create a notification */
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setWhen(System.currentTimeMillis())
.setContentText(notificationContent)
.setContentTitle(notificationTitle)
.setAutoCancel(true)
.setTicker(tickerMessage)
.setContentIntent(pendingIntent);
/** Creating a notification from the notification builder */
Notification notification = notificationBuilder.build();
/** Sending the notification to system.
* The first argument ensures that each notification is having a unique id
* If two notifications share same notification id, then the last notification replaces the first notification
* */
nManager.notify((int)System.currentTimeMillis(), notification);
/** Finishes the execution of this activity */
finish();
// OnDestroy method
// Intent result=new Intent(getApplicationContext(),MainActivity.class);
}
@Override
protected void onDestroy(){
super.onDestroy();
try{
receive.send(getApplicationContext(),c,msg);
//recei
} catch (PendingIntent.CanceledException e) {
Log.d("exception","cancelled");
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
PendingIntent
以使用您的应用程序权限执行某些代码。对于尝试执行代码的应用/服务,它不是startActivityForResult
,而只是StartActivity
。
有一种替代方法:使用LocalBroadcastManager
广播您要发回的数据,PendingIntent活动的父活动将接收它(可能在其onCreate()
中)。如果这有帮助,请告诉我。