Straight ashamed to write about it. Please tell me how to check the operation of BroadcastReceiver by logging. I insert the code immediately after the onReceive method, red underlines getPackageManager

Here is the receiver class

public class SmsReceiver extends BroadcastReceiver { private static final String SMS_REC_ACTION = "android.provider.Telephony.SMS_RECEIVED"; private static final String LOG_TAG = "myLogs"; @Override public void onReceive(final Context context, final Intent intent) { Intent intent1 = new Intent("android.provider.Telephony.SMS_RECEIVED"); List<ResolveInfo> infos = getPackageManager().queryBroadcastReceivers(intent1, 0); for (ResolveInfo info : infos) { Log.d(LOG_TAG, "Receiver name: " + info.activityInfo.name+ "; priority= " + info.priority); } if (intent.getAction().equals(SmsReceiver.SMS_REC_ACTION)){ StringBuilder sb = new StringBuilder(); Bundle bundle = intent.getExtras(); if (bundle != null){ Object[] pdus = (Object[]) bundle.get("pdus"); for (Object pdu : pdus) { SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu); sb.append("\nAdres: " + smsMessage.getOriginatingAddress()); sb.append("\nAdres: " + smsMessage.getMessageBody()); } } Toast.makeText(context, "Sms Resiver mesage" + sb.toString(), Toast.LENGTH_LONG).show();}}} 

As they already explained to me, Toast failed, I need to check this by logging.

    2 answers 2

    This is done like this:

     private final static String TAG=SmsReceiver.class.getName(); //blah-blah public void onReceive(final Context context, final Intent intent) { //blah-blah Log.i(TAG, "Ye-ye, receiver started!"); Log.i(TAG, "Intent="+intent.toString()+ " with action="+intent.getAction()); } 

    Then go to the logs displayed by your Eclipse (or whatever you have) and turn on the filter on the tags "SmsReceiver" - and see if your receiver is working or not.

    • Dealt with logs. You are probably the most experienced here, I would like to know from you. They sent me the receiver code here, checked it works on 4.3, but the fact is that the Activiti file is there and it is registered in the manifest. There are two methods onCreate and onCreateOptionsMenu. There is a shortcut in the tab of the application, it can be tampered there, and the Activation window appears at startup. There is nothing in it, but in my opinion it is superfluous. So the question is without the Mine class ..., which is inherited from Activiti, is it possible to launch the receiver, and its correct operation? To avoid this window. If anything, please do not be nervous. I'm just a beginner. - FFFNikolay
    • Make out in the form of a question - Barmaley
    • You can not answer, already done without activit, just removed it from the manifest, Thank you for your attention. - FFFNikolay
    • Thanks for the permission :) What you did is not exactly ice. When you try to open an Activity , it will cause a system crash (since there is no manifest in the manifest). Good luck. - Barmaley
    • And I will not need to open the Activation, I transfer everything from the receiver to the service and the receiver closes. Do I even have everything in the background or does it not matter? - FFFNikolay

    And you wondered what class the getPackageManager() method belongs to?
    so, I recommend you read
    A class BroadcastReceiver , as you know, is not inherited from the class Context . From here it follows why it does not find the getPackageManager() method.
    In onReceive() , the Context is passed to you, so call context.getPackageManager()

    • Thanks, of course, there are probably a lot of interesting things on this link, but I'm not very friendly with English - at the on off level. I will continue to read the theory, I think I will understand Context and others like it. - FFFNikolay
    • @FFFNikolay It is very difficult to develop in programming without knowledge of English. And reading English. manuals, holding a translator at hand, this knowledge can be tightened. - nilagor