Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从MainActivity的OnActivityResult中的挂起意图中获取结果?_Android_Android Pendingintent_Onactivityresult - Fatal编程技术网

Android 如何从MainActivity的OnActivityResult中的挂起意图中获取结果?

Android 如何从MainActivity的OnActivityResult中的挂起意图中获取结果?,android,android-pendingintent,onactivityresult,Android,Android Pendingintent,Onactivityresult,如何从MainActivity的OnActivityResult中的挂起意图(从创建它的位置)中获取结果?我一直在尝试使用createPendingreult(…),但没有多大成功 所采用的方法基于问题 我在运行时在onDestroy函数中得到一个nullpointerException /*code snippet from MainActivity.java*/ public void processRoute() { Intent proximityInt

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

所采用的方法基于问题

我在运行时在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");

    }

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

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

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