来自AlarmManager的PendingIntent未激活

时间:2018-05-06 02:09:29

标签: android broadcastreceiver alarmmanager android-context android-pendingintent

我有一个测试应用程序,在按下按钮时,应该在按下按钮一分钟后使用AlarmManager来呼叫服务。下面是MainActivity类的代码

public class MainScreen extends AppCompatActivity {
    private View mContentView;

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

        setContentView(R.layout.activity_main_screen);

        mContentView = findViewById(R.id.fullscreen_content);

    }

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

    protected void onResume(){
        super.onResume();
        hideBar();
    }

    public void onWindowFocusChanged(boolean b){
        super.onWindowFocusChanged(b);
        hideBar();
    }

    private void hideBar(){
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    protected void press(View view){


        Intent alI = new Intent(getBaseContext(),ARec.class);
        PendingIntent pi = PendingIntent.getService(getBaseContext(), 126, alI, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000*60, pi);
        Log.d("Main","Alarm set");

    }

    public static class ARec extends BroadcastReceiver{
        public ARec(){
            super();
            Log.d("Rec","Constructed");
        }
        public void onReceive(Context c, Intent i){
            Toast.makeText(c,"Receiver",Toast.LENGTH_LONG);
            Log.d("Rec","Received");
        }
    }

}

此活动的布局位于以下XML文件中。它只是一个按下按钮,被称为press()

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context=".MainScreen">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/dummy_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button"
            android:onClick="press"/>

    </LinearLayout>
</FrameLayout>

最后,我们有清单文件。如您所见,我将ARec注册为接收器,因此我认为这应该不是问题。我还包括了我想象的这个测试应用程序所需的任何权限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.funguscow.testapp">

    <uses-sdk android:minSdkVersion="23" android:maxSdkVersion="27" android:targetSdkVersion="26" />

    <uses-permission android:name="android.permission.SET_ALARM" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainScreen"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:process=":remote" android:enabled="true" android:name=".MainScreen$ARec" />
    </application>

</manifest>

我是在运行Android 8.1的Pixel API 27模拟器上从Android Studio运行的。鉴于我在MainScreen类中的代码,我希望在按下按钮后,我会看到&#34; Alarm set&#34;在日志中,然后60秒后,我会看到&#34;收到&#34;在日志中,以及模拟器上的吐司。虽然我看到&#34;闹钟设置&#34;按下按钮时的消息,我从未看到任何表示收到意图的内容。没有&#34;收到&#34;消息,没有吐司。我尝试将set替换为setExact,没有区别。我将闹钟类型设置为RTC_WAKEUP,但是当它应该激活时屏幕亮起,所以我认为这不应该有所作为。为什么我设置为AlarmManager的意图在60秒后发生(我也尝试过其他时间间隔),从未发生过?

0 个答案:

没有答案