【问题标题】:Activity is opening multiple times using broadcast receiver活动使用广播接收器多次打开
【发布时间】:2016-05-02 11:23:11
【问题描述】:

我正在开发 android 应用程序,它用作电池指示器,当电池电量不足或充满时打开活动。

当我在服务启动后从 onCreate 事件调用 Main Activity 中的 finish() 时,它工作正常。

但是当我评论完成方法时,它会多次打开活动,我从广播接收器打开。

这是 MainActivity 代码:

public class Main extends Activity {
private MyService service;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

if (service == null) {

    Intent i = new Intent(this, MyService.class);
    startService(i);
}

finish();
}}

这是我的服务代码: 当我开始活动时,我认为我做错了什么。 在行

getApplication().startActivity(intent);

完整的服务代码是:

public class MyService extends Service{

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("MyService", "onStartCommand");
    // do not receive all available system information (it is a filter!)
    final IntentFilter battChangeFilter = new IntentFilter(
            Intent.ACTION_BATTERY_CHANGED);
    // register our receiver
    this.registerReceiver(this.batteryChangeReceiver, battChangeFilter);
    return super.onStartCommand(intent, flags, startId);
}
private final BroadcastReceiver batteryChangeReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        checkBatteryLevel(intent);
    }
};

private void checkBatteryLevel(Intent batteryChangeIntent) {
    // some calculations
    final int currLevel = batteryChangeIntent.getIntExtra(
            BatteryManager.EXTRA_LEVEL, -1);
    final int maxLevel = batteryChangeIntent.getIntExtra(
            BatteryManager.EXTRA_SCALE, -1);
    final int percentage = (int) Math.round((currLevel * 100.0) / maxLevel);

    if(percentage==100)
    {
        Intent intent = new Intent(getBaseContext(), Last.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(intent);

    }
    if(percentage==15)
    {
        Intent intent = new Intent(getBaseContext(), Last.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(intent);

    }
    }}

这是我最后一次打开多次的“Last.cs”活动。 但是当我将 finish() 调用到 Main Activity 时它工作正常。

public class Last extends Activity {
Button btnCancel;
Uri notification;
Ringtone r;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last);

notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();


 btnCancel = (Button) findViewById(R.id.stopsound);

 btnCancel.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

         r.stop();
    }
});

}  }

【问题讨论】:

  • 代码太多。只发布必要的部分
  • 你可以在activity中实现广播接收器,为什么要使用service来实现广播
  • @Clairvoyant 如果我关闭活动或应用程序,那么我如何接收广播??

标签: java android android-activity broadcast android-broadcastreceiver


【解决方案1】:

在清单中将您的上次活动启动模式设置为 singleTask

 <activity
      android:name=".Last"
      android:configChanges="orientation|screenSize"
      android:launchMode="singleTask"
      >
  </activity>

【讨论】:

  • 如果这个答案可以帮助您避免创建多个活动。请接受它作为答案,它可能对面临同样问题的其他人有用。
  • 但是为什么当我在主要活动上调用完成时它会起作用??
  • getApplication().startActivity(intent)是否有问题;在服务代码中??
  • 另一个问题.. 是否会重新启动活动??我什么时候想要??
  • 花点时间了解developer.android.com/guide/components/…中的launchMode,如果你不明白,请问我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多