gpt4 book ai didi

android - 使用 BroadcastReceiver 安装 .apk 文件后,BroadcastReceiver 会继续运行吗?

转载 作者:行者123 更新时间:2023-11-30 03:17:16 29 4
gpt4 key购买 nike

我认为即使我退出应用程序,BroadcastReceiver AlarmManagerBroadcastReceiver 也会继续运行。我认为 BroadcastReceiver 会在我重新启动系统时自动恢复并继续运行。为了停止广播接收器,我必须卸载应用程序,对吧?

布局

<LinearLayout 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:orientation='vertical'>

<Button
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:padding='@dimen/padding_medium'
android:text='@string/start_repeating_alarm'
android:onClick='startRepeatingAlarm'
tools:context='.EnableDisableBroadcastReceiver' />

<Button
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:padding='@dimen/padding_medium'
android:text='@string/cancel_alarm'
android:onClick='cancelAlarm'
tools:context='.EnableDisableBroadcastReceiver' />

<Button
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:padding='@dimen/padding_medium'
android:text='@string/enable_broadcast_receiver'
android:onClick='enableBroadcastReceiver'
tools:context='.EnableDisableBroadcastReceiver' />

<Button
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:padding='@dimen/padding_medium'
android:text='@string/disable_broadcast_receiver'
android:onClick='disableBroadcastReceiver'
tools:context='.EnableDisableBroadcastReceiver' />

</LinearLayout>

资源

<resources>
<string name='app_name'>EnableDisableBroadcastReceiver</string>
<string name='enable_broadcast_receiver'>Enable Broadcast Receiver</string>
<string name='disable_broadcast_receiver'>Disable Broadcast Receiver</string>
<string name='start_repeating_alarm'>Start Repeating Alarm</string>
<string name='cancel_alarm'>Cancel Alarm</string>
<string name='menu_settings'>Settings</string>
<string name='title_activity_enable_disable_boradcast_receiver'>EnableDisableBoradcastReceiver</string>
</resources>

广播接收器

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

final public static String ONE_TIME = 'onetime';

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

//You can do the processing here update the widget/remote views.
StringBuilder msgStr = new StringBuilder();
//Format time.
Format formatter = new SimpleDateFormat('hh:mm:ss a');
msgStr.append(formatter.format(new Date()));

Toast.makeText(context, msgStr, Toast.LENGTH_SHORT).show();

}
}

Activity

public class EnableDisableBroadcastReceiver extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void startRepeatingAlarm(View view)
{
AlarmManager am=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
//After after 2 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 4 , pi);
Toast.makeText(this, 'Started Repeating Alarm', Toast.LENGTH_SHORT).show();
}

public void cancelAlarm(View view)
{
Intent intent = new Intent(this, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
Toast.makeText(this, 'Cancelled alarm', Toast.LENGTH_SHORT).show();
}

public void enableBroadcastReceiver(View view){
ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
PackageManager pm = this.getPackageManager();

pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(this, 'Enabled broadcast receiver', Toast.LENGTH_SHORT).show();
}

public void disableBroadcastReceiver(View view){
ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
PackageManager pm = this.getPackageManager();

pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(this, 'Disabled broadcst receiver', Toast.LENGTH_SHORT).show();
}
}

最佳答案

阅读Broadcastreceiver and Paused activity .它提到当您在 Activity 中以编程方式注册广播接收器时,当 Activity 暂停时它不会收到广播。

您可能会想要使用 Service如果你想在后台运行它。

A Service is an application component that can perform long-running operations in the background and does not provide a user interface.

让服务运行并在那里注册 BroadcastReceiver。您可以在 list 中注册它。如果你也想在重启时运行它,那么你需要使用:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

并将其放入接收器的 intent-filter 中:

<receiver android:enabled="true"           
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

如果可能,您可以改为在 list 中注册广播接收器并在那里处理它。接收器在主线程上运行,因此理想情况下,您可以向正在运行的服务发出信号或在那里启动服务。您可能想阅读 Should an alarm be built with Service or BroadcastReceiver? .

希望对您有所帮助。

关于android - 使用 BroadcastReceiver 安装 .apk 文件后,BroadcastReceiver 会继续运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827101/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com