启动myActivity时action_power_connected不起作用

时间:2019-04-21 06:50:53

标签: android

在我的活动中,有一个动画来通知已连接的费用。如果应用程序在连接充电的情况下启动,则没有动画。如果重新连接,动画将打开。我的活动开始后需要立即检查充电连接。

清单

<activity
   android:name=".secondactivity"
   android:label="secondactivity">
   <intent-filter>
       <action android:name="com.test.myApp.secondactivity" />
       <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
       <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
       <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

SecondActivity

public class secondactivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.secondactivity);
    }

    ChargeBroadcast chargeBroadcast;

    @Override
    protected void onResume() {
        super.onResume();

        chargeBroadcast = new ChargeBroadcast();

        IntentFilter intentFilter = new IntentFilter();

        intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);

        intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);

        registerReceiver(chargeBroadcast, intentFilter);

    }

    public class ChargeBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(intent.ACTION_POWER_CONNECTED)) {
                ImageView charge = (ImageView) findViewById(R.id.charge);
                AnimationDrawable animcharge = (AnimationDrawable) charge.getDrawable();
                animcharge.stop();

            } else if (intent.getAction().equals(intent.ACTION_POWER_DISCONNECTED))
                ImageView charge = (ImageView) findViewById(R.id.charge);
                AnimationDrawable animcharge = (AnimationDrawable) charge.getDrawable();
                animcharge.start();
            }

        }
    }
}

secondActivity启动时,应开始或不启动动画的充电连接检查(取决于是否连接了充电器)。有了这段代码,我在第二个活动的开始就没有动画了,如果重新连接充电器,动画就会开始。

0 个答案:

没有答案