gpt4 book ai didi

android - Intent 过滤器甚至不会以编程方式注销

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:04 26 4
gpt4 key购买 nike

我想在收到短信时 toast 。

我尝试明确地将 intent 过滤器放在 list 中 并且我部分成功了,除了我不想在应用程序被终止时调用它,所以在一些程序员的建议下,我尝试通过在我的用户界面中放置两个按钮以编程方式制作广播接收器,即 registerunregister,因此它们的唯一目的是注册和注销广播接收器。

我的主要目标是运行广播接收器,即使应用程序是后台(满足条件,用户按下注册并在使用另一个应用程序之后)。
我一直在使用这个教程:http://www.javacodegeeks.com/2012/09/android-broadcast-receiver.html , 但基本上用短信接收器修改了它。

这是我的代码:

package gates.apps.automaticmessageresponder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

SmsReceiver broadcastReceiver=new SmsReceiver();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void register(View view){

this.registerReceiver(broadcastReceiver, new IntentFilter(
"android.provider.Telephony.SMS_RECEIVED"));
Log.e("register","pressed");

}
public void unRegister(View view){

this.unregisterReceiver(broadcastReceiver);
Log.e("unregister","pressed");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

还有其他类

package gates.apps.automaticmessageresponder;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();

try {

if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();

String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();

Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


// Show alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();

} // end for loop
} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}

我没有修改 list 文件,只是添加了接收短信的权限。即使我正在登录,寄存器也被按下和未按下。我认为问题在于注册接收器按钮即使没有点击也会被调用,这是异常行为吗?还是我的认知有误?

最佳答案

与其在 list 中声明 BroadcastReceiver,不如在您的 Activity 中尝试如下,这会将其与 Activity 生命周期联系起来(不要忘记在 list 文件中添加权限) :

public class ExampleMainActivity extends Activity {

//Activity Stuff

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, final Intent intent) {

//Do things you want with message.

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Title");
builder.setMessage("You may add your things here");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();
}
});
builder.show();
}
};

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

// OR YOU CAN REGISTER UNREGISTER AS YOU WANT

this.registerReceiver(broadcastReceiver, new IntentFilter(
"android.provider.Telephony.SMS_RECEIVED"));
};

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

// OR YOU CAN REGISTER UNREGISTER AS YOU WANT

this.unregisterReceiver(broadcastReceiver);
}
}

编辑:

您可以使用应用程序首选项检查此替代解决方案:

package gates.apps.automaticmessageresponder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void register(View view){

SharedPreferences preferences = context.getSharedPreferences("FILE_NAME", Context.MODE_PRIVATE);
Editor edit = preferences.edit();
edit.putBoolean("isRegistered", true);
edit.commit();

Log.e("register","pressed");

}
public void unRegister(View view){

SharedPreferences preferences = context.getSharedPreferences("FILE_NAME", Context.MODE_PRIVATE);
Editor edit = preferences.edit();
edit.putBoolean("isRegistered", false);
edit.commit();

Log.e("unregister","pressed");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

package gates.apps.automaticmessageresponder;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();

try {

if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();

String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();

Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);

SharedPreferences preferences = context.getSharedPreferences("FILE_NAME", Context.MODE_PRIVATE);
boolean isRegistered = preferences.getBoolean("isRegistered", false);

if(isRegistered ) {
// Show alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ",
message: " + message, duration);
toast.show();
}

} // end for loop
} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);

}

}

}

关于android - Intent 过滤器甚至不会以编程方式注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21603400/

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