代码之家  ›  专栏  ›  技术社区  ›  Fahad Shahid

AlarmManager挂起意图不与广播接收器一起工作

  •  3
  • Fahad Shahid  · 技术社区  · 6 年前

    我正在开发一个要设置自定义警报的应用程序。但在设置了警报时间后,什么也没有发生。我正在共享我的代码。如果我的代码中缺少某些内容,请帮助和指导我。

    在我的主要活动中。Java语言

            @Override
              protected Dialog onCreateDialog(int id) {
    
                    if (id == DIALOG_ID) {
            return new TimePickerDialog(MainActivity.this, kTimePickerListener, hour, min, false);
        }
        return null;
    }
    
    protected TimePickerDialog.OnTimeSetListener kTimePickerListener =
            new TimePickerDialog.OnTimeSetListener() {
    
                @Override
                public void onTimeSet(TimePicker timePicker, int hourofday, int minutes) {
                    hour = hourofday;
                    min = minutes;
    
                    Calendar calendar = Calendar.getInstance();
    
                    // set selected time from timepicker to calendar
    
                    calendar.set(Calendar.HOUR_OF_DAY, hour);
                    calendar.set(Calendar.MINUTE, minutes);
    
                    Intent myIntent = new Intent(MainActivity.this, ServiceManager.class);
                    myIntent.putExtra("reqcode", 11);
    
                    // A PendingIntent specifies an action to take in the
                    // future
                    PendingIntent mPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 11, myIntent, 0);
    
                    // set alarm time
                     alarmManager = (AlarmManager) MainActivity.this
                            .getSystemService(Context.ALARM_SERVICE);
                    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), mPendingIntent);
    
                }
            };
    

    在我的ServiceManager中。班

               public class ServiceManager extends BroadcastReceiver {
    
    
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        int Noti_code = intent.getIntExtra("reqcode",-1);
    
    
            Intent myIntent = new Intent(context, NotificationService.class);
            myIntent.putExtra("reqcode", Noti_code);
            context.startService(myIntent);
    
    
    }
    }
    

    在我的NotificationService中。班

        public class NotificationService extends Service {
    
    private NotificationManager mManager;
    
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
    }
    
    @SuppressWarnings({"static-access", "deprecation"})
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    
        int Noti_Code = intent.getIntExtra("reqcode",-2);
    
        if (Noti_Code == 11) {
            mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
            ShowNotification(getApplicationContext(), mManager);
        } 
    
    
    }
    
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
    }
    

    在我的舱单上。xml

         <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity_detail" />
        <activity android:name=".History" />
    
        <service
            android:name=".NotificationService"
            android:enabled="true"
            android:exported="true"
            android:stopWithTask="false"/>
        <receiver android:name=".ServiceManager"
            android:process=":remote"/>
    </application>
    

    我正在调试广播接收器未调用。如果我遗漏了什么,请帮助我。 我还想知道一次可以发射多少个待定的意图,它们会一个接一个地工作吗?

    谢谢

    参考链接:

    AlarmManager wont fire pending intent

    http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/

    BroadcastReceiver and AlarmManager not working with

    AlarmManager is not working after app is closed? - Android

    1 回复  |  直到 6 年前
        1
  •  2
  •   David Wasser    6 年前

    你有你的 BroadcastReceiver 正在另一个进程中运行。如果使用调试器设置断点,则会错过此操作。如果你不需要 广播接收机 在单独的进程中运行,删除

    android:process=":remote"
    

    清单中的声明。

    请参见 documentation 关于 android:process 说明符起作用。