2015-04-16 35 views
1

如何從MainActivity的OnActivityResult(從創建它的位置)的掛起的Intent中獲取結果?我一直在試圖用creatependingresult(...),但沒有成功從MainActivity的OnActivityResult中的掛起Intent中獲取結果?

採用的是基於這個答案的方法: - Start PendingIntent for result

我在運行時

湊了功能的onDestroy一個NullPointerException

從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//代碼段包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

PendingIntent被用於其他應用/服務創建與您的應用程序的權限執行一些代碼。這不是startActivityForResult,而是嘗試執行您的代碼的應用程序/服務的StartActivity

還有一個替代方案:使用LocalBroadcastManager廣播要發回的數據,PendingIntent活動的父活動將接收它(可能在其onCreate()中)。讓我知道這是否有幫助。

相關問題