Android Intent.getStringExtra() 在 Broadcastreceiver 中返回 null

Posted

技术标签:

【中文标题】Android Intent.getStringExtra() 在 Broadcastreceiver 中返回 null【英文标题】:Android Intent.getStringExtra() returns null in Broadcastreceiver 【发布时间】:2016-10-25 23:43:45 【问题描述】:

我创建了一项服务,该服务在位置更改后生成意图并将位置数据放入其中。

intent.putExtra("Latitude", String.valueOf(loc.getLatitude())); //Double
            intent.putExtra("Longitude", String.valueOf(loc.getLongitude())); //Double
            intent.putExtra("Provider", loc.getProvider()); //String
            intent.putExtra("Altitude", loc.getAltitude());//Double
            intent.putExtra("Accuracy", loc.getAccuracy()); //float
            intent.putExtra("Speed", loc.getSpeed()); //float

现在,我有一个 Broadcastreceiver,它每 30 秒触发一次,用于将该信息发布到服务器。但是,当我尝试获取 Intent 时:

        Intent myIntent = new Intent(context, GPSService.class); //GPSService is the service
        String lat = (String) myIntent.getExtras().get("Latitude");
        String longi = (String) myIntent.getExtras().get("Longitude");

我收到NullPointerException

尝试调用虚拟方法'java.lang.Object android.os.Bundle.get(java.lang.String)' 在空对象引用上

这是为什么呢?我检查了其他解决方案:有人说检查我是否将正确的 Dateformat 放入意图中(在这种情况下是字符串),我这样做了,有人说使用getIntent().getExtras().getString("aKey"); 我也尝试过,如您在上面看到的,但是我仍然从字符串中得到空值。

将它传递给广播接收器时一定有一些问题,因为我在执行intent.putExtra() 之后记录了这些值,它们绝对不为空。

编辑:

GPS服务:

    package com.dandddeveloper.alarmisto;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class GPSService extends Service

    public static final String BROADCAST_ACTION = "Hello World";
    private static final int TWO_MINUTES = 1000 * 60 * 2;
    public LocationManager locationManager;
    public MyLocationListener listener;
    public Location previousBestLocation = null;

    Intent intent;
    int counter = 0;

    @Override
    public void onCreate()
    
        super.onCreate();
        intent = new Intent(BROADCAST_ACTION);
    

    @Override
    public void onStart(Intent intent, int startId)
    
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        listener = new MyLocationListener();
        //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
    

    @Override
    public IBinder onBind(Intent intent)
    
        return null;
    

    protected boolean isBetterLocation(Location location, Location currentBestLocation) 
        if (currentBestLocation == null) 
            // A new location is always better than no location
            return true;
        

        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
        boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
        boolean isNewer = timeDelta > 0;

        // If it's been more than two minutes since the current location, use the new location
        // because the user has likely moved
        if (isSignificantlyNewer) 
            return true;
            // If the new location is more than two minutes older, it must be worse
         else if (isSignificantlyOlder) 
            return false;
        

        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;

        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(),
                currentBestLocation.getProvider());

        // Determine location quality using a combination of timeliness and accuracy
        if (isMoreAccurate) 
            return true;
         else if (isNewer && !isLessAccurate) 
            return true;
         else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) 
            return true;
        
        return false;
    



    /** Checks whether two providers are the same */
    private boolean isSameProvider(String provider1, String provider2) 
        if (provider1 == null) 
            return provider2 == null;
        
        return provider1.equals(provider2);
    



    @Override
    public void onDestroy() 
        // handler.removeCallbacks(sendUpdatesToUI);
        super.onDestroy();
        Log.v("STOP_SERVICE", "DONE");
        locationManager.removeUpdates(listener);
    

    public static Thread performOnBackgroundThread(final Runnable runnable) 
        final Thread t = new Thread() 
            @Override
            public void run() 
                try 
                    runnable.run();
                 finally 

                
            
        ;
        t.start();
        return t;
    




    public class MyLocationListener implements LocationListener
    

        public void onLocationChanged(final Location loc)
        
            Log.i("***********************", "Location changed");
            if(isBetterLocation(loc, previousBestLocation)) 
                loc.getLatitude();
                loc.getLongitude();
                intent.putExtra("Latitude", String.valueOf(loc.getLatitude())); //Double
                intent.putExtra("Longitude", String.valueOf(loc.getLongitude())); //Double
                intent.putExtra("Provider", loc.getProvider()); //String
                intent.putExtra("Altitude", loc.getAltitude());//Double
                intent.putExtra("Accuracy", loc.getAccuracy()); //float
                intent.putExtra("Speed", loc.getSpeed()); //float
                intent.putExtra("Bearing", loc.getBearing()); //float
                Log.v("LOC", String.valueOf(loc.getLatitude()));
                Log.v("LOC", String.valueOf(loc.getLongitude()));
                Log.v("LOC", loc.getProvider());
                Log.v("LOC", String.valueOf(loc.getAltitude()));
                Log.v("LOC", String.valueOf(loc.getAccuracy()));
                Log.v("LOC", String.valueOf(loc.getSpeed()));
                Log.v("LOC", String.valueOf(loc.getBearing()));

                sendBroadcast(intent);

            
        

        public void onProviderDisabled(String provider)
        
            Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
        


        public void onProviderEnabled(String provider)
        
            Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
        


        public void onStatusChanged(String provider, int status, Bundle extras)
        

        

    

调用MainActivity中的Alarmreceiver:

Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);

manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);

【问题讨论】:

我可以看看你的两个类的完整代码吗?? @SouravChandra 当然,添加了必要的代码。如果您想知道为什么这不是来自 Broadcastreceiver 的所有内容,实际上,没有任何其他与从服务中获取意图相关的内容。这是一个典型的public class AlarmReceiver extends WakefulBroadcastReceiverpublic void onReceive(Context context, Intent intent) 而是创建一个新的 Intent。您应该以 private AlarmReceiver mMessageReceiver = new AlarmReceiver() @Override public void onReceive(Context context, Intent intent) // 提取 Intent 中包含的数据的形式接收 Intent ;假设 AlarmReceiver 扩展了 BroadcastReceiver。不知道你有没有关注。 @SouravChadra 所以我必须在我的Alarmreceiver 中创建一个Alarmreceiver?我不确定你是否理解我每 30 秒从我的MainActivity 调用一次 Alarmreceiver 类。在Alarmreceiver 里面,我想从GPSservice 获取数据。 好的,我明白你的问题了。我将用潜在的解决方案更新我的答案。我以为您的 MainActivity 中已经有一个广播接收器对象。 【参考方案1】:

MainActivity 类中创建一个BroadcastReceiver 对象,如下所示:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() 
    @Override
    public void onReceive(Context context, Intent intent) 
        // Extract data included in the Intent

    
;

还不是在MainActivity 中使用定期检查,而是创建一个Timer 对象并使用sendBroadcast(intent) 发送定时广播。

【讨论】:

Op 已经提到getIntent().getExtras().getString("aKey"); 也不起作用。 @SouravChandra 我可以在我的AlarmReceiver BroadcastReceiver 中使用这个BroadcastReceiver 吗?如果我理解您的解决方案,我将不得不将意图传递给这个私有 mMessageReceiver,提取它,然后以某种方式将其传递给另一个 BroadcastReceiver? 没有朋友,只需在 MainActivity 中使用 sn-p。 sendBroadcast 负责发送部分,接收是通过此 BroadcastReceiver 对象的参数完成的。 当然可以,但是我如何处理我在 mMessageReceiver 中得到的字符串? 是的,您可以在 AlarmReceiver 中使用广播接收器【参考方案2】:

receiver必须有onreceieve方法。所以使用它的intent并使用intent.getStringExtra("key");并使用它。

【讨论】:

我尝试在onReceive中调用上面的代码(广播接收器中的部分)。你的意思是我不能在广播接收器中创建新的意图并从它们那里获得额外的东西吗?

以上是关于Android Intent.getStringExtra() 在 Broadcastreceiver 中返回 null的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )

android 21 是啥版本

Android逆向-Android基础逆向(2-2)

【Android笔记】android Toast

图解Android - Android核心机制

Android游戏开发大全的目录