【问题标题】:Sending Extra Data from BroadcastReceiver Context is Null从 BroadcastReceiver 上下文发送额外数据为 Null
【发布时间】:2015-08-18 15:42:22
【问题描述】:

我只是想弄清楚服务、广播接收器和所有这些东西是如何工作的。

我写了一个接收器,当屏幕状态改变时触发。然后它会打开一个服务,该服务会打开一个放置在锁定屏幕上的 Activity。只要我的应用程序正在运行,它就可以正常工作,但是当我的应用程序死机时,传递给我的 BroadcastReceiver 的上下文为空。而且我无法将我的数据传输到服务,因为 Intent 也为空。

我希望有人知道如何解决这个问题。

我的代码如下所示:

启动服务的活动:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent service = new Intent(this,UpdateService.class);
    startService(service);

    setContentView(R.layout.activity_main);
}

服务:

public class UpdateService extends Service {
@Override
public void onCreate() {
    super.onCreate();
    // REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);
}

@Override
public int onStartCommand(Intent intent,int flags , int startId) {
    boolean screenOn = intent.getBooleanExtra("screen_state", false);
    if (!screenOn) {
        // Screen is on
    } else {
        // Screen is off
        Intent myIntent = new Intent(this,MyActivity.class);
        // The Flag is important to open an Acitivy from the service.
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(myIntent);
    }
    return Service.START_STICKY;
}

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

}

广播接收器:

public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        screenOff = true;
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        screenOff = false;
    }
    Intent intent2open;
    intent2open = new Intent(context, UpdateService.class);
    intent2open.putExtra("screen_state", screenOff);
    context.startService(intent2open);
}

}

【问题讨论】:

  • 只是问,但你打算如何在锁定屏幕上启动你的活动?
  • 计划是,Service 使用以下代码行启动 Activity: Intent myIntent = new Intent(this,MyActivity.class); // 标志对于从服务中打开 Activity 很重要。 myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(myIntent);

标签: android android-intent service broadcastreceiver


【解决方案1】:

我认为您最好直接在清单中注册您的接收器并将this 更改为getApplicationContext() 或启动一个活动以启动Intent 但确保它使用finish() 方法所以用户不会甚至知道调用了另一个活动!

编辑:

我想我明白你的问题是什么。我已经修改了您的代码并删除了该服务,因为我认为它不是必需的。

  @Override
protected void onCreate(Bundle.savedInstanceState) {

public boolean ScreenState =     true; 
super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);
}

protected void onNewIntent(Intent    intent) { 
 ScreenState =     intent.getExtra("screen_state"))
if(ScreenState==false)
 { 
      //**Do whatever you want**
 }
 else
{
     //False alarm close the activity.
     finish();
}
}

您的广播接收器。

public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;

@Override
public void onReceive(Context.   context, Intent intent){

if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
    screenOff = true;
  } else if.   (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
    screenOff = false;
  }
Intent intent2open;
intent2open = new Intent(context, **My Activity**.class);
   intent2open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent2open.putExtra("screen_state", screenOff);
context.startActivity(intent2open);
}
}

让我知道这是否有效。 记住广播接收器将 My Activity 更改为您的活动名称。

【讨论】:

  • 您好,谢谢您的回答。我的接收器已在清单中注册,服务也是如此: 这是哪个你的意思是?在服务中还是在活动中?这对我收到空对象的问题有帮助吗?
  • 如果它已经在清单中注册,你为什么还要在服务中注册。您想要做的就是每当屏幕打开或打开活动时,对吗?
  • @Eric 上面的代码应该可以工作,请查看编辑并让我知道它是否有效。
  • 您好 KISHORE_ZE,感谢您的努力。我不知道应该在哪里放置 onNewIntent 方法。我尝试在没有服务的情况下使用代码并且它有效,但仅当应用程序在后台运行时。但我不希望应用程序在后台运行。应该如下:用户启动应用程序一次并看到活动“A”,然后关闭应用程序。比,总是当他关闭屏幕时,活动“B”被启动,它被放置在锁屏的前面。但是当应用程序关闭时,BroadcastReceiver 接收到的 Context 为空,而我无法传输 Screenstate。
  • onNewIntent 被放置在类中。在onCreate() 之前,如果您想要一个特定的地方。好的,那么让我自己编译这段代码并检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 2014-02-23
  • 2014-06-04
相关资源
最近更新 更多