vity的悬而未决的onActivityResult得到意向结果

vity的悬而未决的onActivityResult得到意向结果

本文介绍了从MainActivity的悬而未决的onActivityResult得到意向结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如何获得在MainActivity的悬而未决的onActivityResult导致意向(从创建它)?我一直在试图用creatependingresult(...)没有取得多大成功。

使用的方法是基于这样的回答: -

我在运行时得到一个NullPointerException异常的函数的onDestroy

/ 从MainActivity.java code段 /

 公共无效processRoute(){
        意图proximityIntent =新意图(getApplicationContext(),ProximityActivity.class);
        调用getResult = this.createPendingResult(计数器,新意图(),PendingIntent.FLAG_CANCEL_CURRENT);
        Log.d(得到结果,完成);
        包裹P = Parcel.obtain();
        p.writeValue(调用getResult);
        parcelsend = MyParcel.CREATOR.createFromParcel(对);        proximityIntent.putExtra(attachedIntentParcelable,parcelsend);
        proximityIntent.putExtra(专柜,专柜);
        Log.d(notif,意图附加为parcelable);        的PendingIntent = PendingIntent.getActivity(getBaseContext(),0,proximityIntent,PendingIntent.FLAG_CANCEL_CURRENT);
        locationManager.addProximityAlert(orderedList.x.get(柜).x.latitude,orderedList.x.get(柜).x.longitude,20,-1,的PendingIntent);
        Log.d(警惕补充,接近警戒补充);    }

/ ProximityActivity.java 的/
        包anmol.csp5;

 进口android.app.Activity;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.Context;
进口android.content.Intent;
进口android.location.LocationManager;
进口android.net.Uri;
进口android.os.Bundle;
进口android.os.Parcel;
进口android.support.v4.app.NotificationCompat;
进口android.util.Log;
进口android.widget.Toast;。进口的Andr​​oid preference preferenceManager.OnActivityDestroyListener;公共类ProximityActivity延伸活动{    串notificationTitle;
    串notificationContent;
    串tickerMessage;
    收到的PendingIntent;
    意图味精;
    INT℃;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);        布尔proximity_entering = getIntent()getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING,真)。
        MyParcel receivedParcelable = getIntent()getParcelableExtra(attachedIntentParcelable);
        C = getIntent()getIntExtra(反, - 1);
        包裹REC = Parcel.obtain();
        receivedParcelable.writeToParcel(REC,0);        收到=(的PendingIntent)rec.readValue(PendingIntent.class.getClassLoader());
        Log.d(notif,未决的意图解压);        如果(proximity_entering){
            Toast.makeText(getBaseContext(),输入区域,Toast.LENGTH_LONG).show();
            notificationTitle =感应 - 输入;
            notificationContent =进入本地区;
            tickerMessage =进入本地区;
            Log.d(警戒点,进);
        }其他{
            Toast.makeText(getBaseContext(),退出区域,Toast.LENGTH_LONG).show();
            notificationTitle =感应 - 退出;
            notificationContent =退出区;
            tickerMessage =退出区;
            Log.d(警戒点,退出);
        }        味精=新意图(getApplicationContext(),MainActivity.class);
        msg.putExtra(进入,proximity_entering);
// receivedIntent = PendingIntent.getActivity(getApplicationContext(),计数,送,PendingIntent.FLAG_CANCEL_CURRENT);//尝试{
// receive.send(C);
//}
//赶上(PendingIntent.CanceledException E){
// Log.d(异常,取消);
// e.printStackTrace();
//}        意图notificationIntent =新意图(getApplicationContext(),NotificationView.class);
        notificationIntent.putExtra(内容,notificationContent);        / **这是为了使这种意图不同,它的previous意图* /
        notificationIntent.setData(Uri.parse(电话:/+(INT)System.currentTimeMillis的()));        / **创建为每个通知不同的任务。看升旗Intent.FLAG_ACTIVITY_NEW_TASK * /
        的PendingIntent的PendingIntent = PendingIntent.getActivity(getApplicationContext(),0,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);        / **获取系统服务NotificationManager * /
        NotificationManager nManager =(NotificationManager)getApplicationContext()getSystemService(Context.NOTIFICATION_SERVICE)。        / **配置通知生成器创建的通知* /        NotificationCompat.Builder notificationBuilder =新NotificationCompat.Builder(getApplicationContext())
                .setWhen(System.currentTimeMillis的())
                .setContentText(notificationContent)
                .setContentTitle(notificationTitle)
                .setAutoCancel(真)
                .setTicker(tickerMessage)
                .setContentIntent(的PendingIntent);
        / **从通知建设者*创建通知/
        通知通知= notificationBuilder.build();        / **发送通知到系统中。
         *第一个参数,确保每个通知是有一个唯一的ID
         *如果两个通知共享相同的通知ID,那么最后的通知替换第一个通知
         * * /
        nManager.notify((INT)System.currentTimeMillis的(),通知);        / **结束该活动的执行* /
        完();
        //方法的OnDestroy
//意向书结果=新意图(getApplicationContext(),MainActivity.class);    }
    @覆盖
    保护无效的onDestroy(){
        super.onDestroy();
        尝试{
            receive.send(getApplicationContext(),C,味精);
            // recei
        }赶上(PendingIntent.CanceledException E){
            Log.d(异常,取消);
            e.printStackTrace();
        }
    }
}


解决方案

的PendingIntent 其他应用程序/服务的创建与应用的权限执行某些code。这不是 startActivityForResult ,但只是 StartActivity 为应用程序/服务努力执行你的code

有一个可以替代的:使用 LocalBroadcastManager 广播要发回的数据,你的PendingIntent的活动的父活动将在收到(也许它的onCreate())。让我知道,如果这有助于。

How to get result from pending Intent in OnActivityResult of MainActivity(from where it was created)? I have been trying to use creatependingresult(...) without much success

The approach used is based on this answer:- Start PendingIntent for result

I'm getting a nullpointerException in the onDestroy function at runtime

/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 are created for other applications/service to execute some code with your app permissions. It's not startActivityForResult, but just StartActivity for the app/service trying to execute your code.

There is one alternative to this: Using LocalBroadcastManager to broadcast the data you want to send back, the parent activity of your PendingIntent's activity will receive it (Maybe in its onCreate()). Let me know if this helps.

这篇关于从MainActivity的悬而未决的onActivityResult得到意向结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:09