【问题标题】:Telephony cannot be resolved电话无法解决
【发布时间】:2015-10-16 06:10:18
【问题描述】:

我在 BroadcastReceiver 中使用了此代码,但此处无法解析 Telephony。如何在 BroadcastReceiver 中使用 Telephony?我认为是因为在 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);
         }





            }}}

【问题讨论】:

    标签: android eclipse broadcastreceiver telephony


    【解决方案1】:

    你需要Telephony.Sms.getDefaultSmsPackage(context),而不是Telephony.Sms.getDefaultSmsPackage(this)。在您的情况下,this 指的是 BroadcastReceiver,并且该方法需要一个 context 参数。

    【讨论】:

      猜你喜欢
      • 2017-08-16
      • 2018-04-20
      • 2010-12-25
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 2016-08-02
      • 1970-01-01
      相关资源
      最近更新 更多