如何从 OnActivityActivity中挂起的意图获取结果主活动的结果



如何从MainActivity OnActivityResult(从创建位置)的挂起意图中获取结果?我一直在尝试使用creatependingresult(...)但没有取得多大成功

所使用的方法基于这个问题。

我在运行时在 onDestroy 函数中收到一个空指针异常

/*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();
        }
    }
}
PendingIntent是为

其他应用程序/服务创建的,以使用您的应用程序权限执行某些代码。这不是startActivityForResult,而只是尝试执行代码的应用程序/服务的StartActivity

还有一种替代方法:使用 LocalBroadcastManager 广播您要发回的数据,您的 PendingIntent 活动的父活动将收到它(可能在其onCreate()中)。让我知道这是否有帮助。

最新更新