2015-10-16 79 views
0

我在BroadcastReceiver中使用了此代碼,但這裏無法解決電話問題。我如何在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); 
     } 





      }}} 

回答

0

而不是Telephony.Sms.getDefaultSmsPackage(this)你需要Telephony.Sms.getDefaultSmsPackage(context)。在你的情況下,this引用BroadcastReceiver,並且該方法需要一個context參數。