Java 广播接收器在申请离开后继续
我有一个无法解决的问题,当我离开我的应用程序时,我的应用程序的广播接收器继续运行。我必须手动强制停止应用程序,而我已关闭; 谢谢你的帮助。这是我的舱单和我的接收人:Java 广播接收器在申请离开后继续,java,android,broadcastreceiver,android-manifest,ondestroy,Java,Android,Broadcastreceiver,Android Manifest,Ondestroy,我有一个无法解决的问题,当我离开我的应用程序时,我的应用程序的广播接收器继续运行。我必须手动强制停止应用程序,而我已关闭; 谢谢你的帮助。这是我的舱单和我的接收人: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.locateit.antholife.locateit"> <
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.locateit.antholife.locateit">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@mipmap/locate"
android:label="@string/app_nameprincipal"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<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=".Main2Activity"
android:label="@string/title_activity_main2"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Changelog"
android:label="@string/title_activity_changelog"
android:theme="@style/AppTheme.NoActionBar" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBfmF3WWxsPhufZR5keiDNRy-33hJI1rvM" />
<activity
android:name=".lequipe"
android:label="@string/nomequipe" />
<activity
android:name=".Setting"
android:label="@string/title_activity_setting" />
<activity android:name=".MDPinterne"
android:label="@string/title_activity_mdpsecure" />
<activity android:name=".Bluetooth"
android:label="@string/title_activity_bluetooth" />
<receiver android:name=".MyReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity android:name=".Smsconnection"
android:label="@string/title_activity_gsm" >
</activity>
</application>
我的接收人:
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
public int lock = 0;
@Override
public void onReceive(Context context, Intent intent) {
Bundle extra = intent.getExtras();
if (extra != null) {
Object[] pdus = (Object[]) extra.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
if (messages.length > -1) {
for (int i = 0; i < messages.length; i++) {
String messageBody = messages[i].getMessageBody();
String phoneNumber = messages[i].getDisplayOriginatingAddress();
Toast.makeText(context, "Expéditeur:" + "\n" + phoneNumber, Toast.LENGTH_LONG).show();
Toast.makeText(context, "Message : " + "\n" + messageBody, Toast.LENGTH_LONG).show();
if (messageBody.equals("CS")) {
lock = 1;
SharedPreferences sharedPreferences = context.getSharedPreferences("appSharedPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("lockphone", phoneNumber);
editor.putInt("locksecurity", lock);
editor.commit();
Toast.makeText(context, "Association réussie !", Toast.LENGTH_LONG).show();
Toast.makeText(context, "L'option Connexion GSM est maintenant désactivée.", Toast.LENGTH_LONG).show();
}
/** else {
Toast.makeText(context, "Echec de l'association !", Toast.LENGTH_LONG).show();
Toast.makeText(context, "Merci de renseigner un numéro correct", Toast.LENGTH_LONG).show();
} **/
}
}
}
}
}
公共类MyReceiver扩展了BroadcastReceiver{
公共接收器(){
}
公共int锁=0;
@凌驾
公共void onReceive(上下文、意图){
Bundle extra=intent.getExtras();
如果(额外!=null){
Object[]pdus=(Object[])extra.get(“pdus”);
最终SmsMessage[]消息=新SmsMessage[pdus.length];
对于(int i=0;i-1){
for(int i=0;i
您已在清单中注册了MyReceiver
。因此,它被设置为随时接收广播
如果只希望在某些时间接收广播,请执行以下任一操作:
- 在某些
上下文上使用
和registerReceiver()
,从清单中删除unregisterReceiver()
元素,或者 - 使用
和PackageManager
根据需要启用或禁用setComponentEnabledSetting()
onResume()
中注册了ACTION\u BATTERY\u CHANGED
,并在onPause()中注销:
因此,从输入的角度来看,它将只在片段位于前台时接收广播。并在正确的位置注册/注销。是的,我希望它在应用程序的所有活动上都工作,但我只会在离开应用程序时停止。请让我开始,我也不明白..@Antholife:我不知道“离开应用程序”在技术术语中是什么意思。例如,如果这是单个活动应用程序,您可以在onStart()
中调用registerReceiver()
,在onStop()
中调用unregisterReceiver()
。在最近的应用程序中扫描应用程序时:)是的,但我需要更多详细信息,我是新手,谢谢:)@Antholife:我添加了一个使用registerReceiver()
的示例。是的,谢谢!我稍后再看,谢谢:)您可以按照教程中的操作,也可以像Commonware的答案一样注册/取消注册应用程序中的每个活动。虽然这对应用程序来说可能太重了,但是如果你的活动频繁转换的话。
/***
Copyright (c) 2008-2014 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.battmon;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class BatteryFragment extends Fragment {
private ProgressBar bar=null;
private ImageView status=null;
private TextView level=null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.batt, parent, false);
bar=(ProgressBar)result.findViewById(R.id.bar);
status=(ImageView)result.findViewById(R.id.status);
level=(TextView)result.findViewById(R.id.level);
return(result);
}
@Override
public void onResume() {
super.onResume();
IntentFilter f=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
getActivity().registerReceiver(onBattery, f);
}
@Override
public void onPause() {
getActivity().unregisterReceiver(onBattery);
super.onPause();
}
BroadcastReceiver onBattery=new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
int pct=
100 * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 1)
/ intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
bar.setProgress(pct);
level.setText(String.valueOf(pct));
switch (intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) {
case BatteryManager.BATTERY_STATUS_CHARGING:
status.setImageResource(R.drawable.charging);
break;
case BatteryManager.BATTERY_STATUS_FULL:
int plugged=
intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
if (plugged == BatteryManager.BATTERY_PLUGGED_AC
|| plugged == BatteryManager.BATTERY_PLUGGED_USB) {
status.setImageResource(R.drawable.full);
}
else {
status.setImageResource(R.drawable.unplugged);
}
break;
default:
status.setImageResource(R.drawable.unplugged);
break;
}
}
};
}