I used this code in BroadcastReceiver but here Telephony cannot be resolved. how can I use Telephony here in BroadcastReceiver? I think that it is because of using it in BroadcastReceiver.
public class SmsFilter extends BroadcastReceiver {
SharedPreferences preferences = null ;
Context context;
static ContentResolver ctx;
String text;
@Override
public void onReceive(final Context context, Intent intent) {
if(android.os.Build.VERSION.SDK_INT>= 4.0){
Log.i("LOG", "this is: "+Build.VERSION.RELEASE);
final String myPackageName = context.getPackageName();
if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) {
// App is not default.
// Show the "not currently set as the default SMS app" interface
View viewGroup = findViewById(R.id.not_default_app);
viewGroup.setVisibility(View.VISIBLE);
// Set up a button that allows the user to change the default SMS app
Button button = (Button) findViewById(R.id.change_default_app);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent =
new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
myPackageName);
context.startActivity(intent);
}
});
} else {
// App is the default.
// Hide the "not currently set as the default SMS app" interface
View viewGroup = findViewById(R.id.not_default_app);
viewGroup.setVisibility(View.GONE);
}
}}}
Instead of Telephony.Sms.getDefaultSmsPackage(this)
you need Telephony.Sms.getDefaultSmsPackage(context)
. In your case this
refers to the BroadcastReceiver
, and the method expects a context
argument.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.