This is a simple program i created to test BOOT_COMPLETED event of Android but it's not working! am i doing something wrong here?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.enea.training.bootdemo"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".BootDemoReceiver">
<Intent-filter>
<action android:name ="android.intent.action.BOOT_COMPLETED"></action>
</Intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
java:
public class BootDemoReceiver extends BroadcastReceiver {
static final String TAG = "BootDemoReceiver";
#Override
public void onReceive(final Context context, final Intent bootintent) {
Log.v(TAG, "Come on");
}
}
I'm not sure if it matters, but try changing <Intent-filter> to <intent-filter> (note lowercase 'i').
Related
I've a broadcast receiver and it is not getting hot even though the broadcasts re registered in the manifest.
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trial.trialservice"
android:versionCode="2"
android:versionName="BA_1.00.0.0.001"
android:sharedUserId="android.uid.system" >
<uses-sdk
android:minSdkVersion="21"
/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="false"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Material" >
<service
android:name="com.trial.trialservice.TrialService"
android:exported="true"
android:permission="com.encoding.permission.TRIAL_ACCESS" >
</service>
<receiver android:name=".TrialHandler" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.trialmanager.TRIAL_STATUS_UPDATED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is the handling part of the code:
public class TrialHandler extends BroadcastReceiver {
private final String TAG = "TrialHandler";
private Context mContext;
#Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "Inside TrialHandler onReceive, Intent: "+intent);
mContext = context;
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) ||
intent.getAction().equals("com.trialmanager.TRIAL_STATUS_UPDATED")){
// My business logic goes here
}
}
}
Any help is greatly appreciated. Thanks.
I followed a tutorial on Receiving sms using BroadcastReceiver
https://www.youtube.com/watch?v=h-zYXVODiPo
I used exactly the same codes. Enabled Telnet client. Does my device need to be rooted first? I don't know the problem. Please help.
Here's my MainActivity
public class MainActivity extends Activity {
BroadcastReceiver receiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arr0, Intent arr1) {
processReceiver(arr0,arr1);
}
};
registerReceiver(receiver,filter);
}
public void onDestroy(){
super.onDestroy();
unregisterReceiver(receiver);
}
public void processReceiver(Context context,Intent intent){
Toast.makeText(context,"RECEIVED",Toast.LENGTH_LONG).show();
TextView lbs = (TextView)findViewById(R.id.textView1);
Bundle bundle = intent.getExtras();
Object[] objArr = (Object[])bundle.get("pdus");
String sms="";
for(int i=0;i>objArr.length;i++){
SmsMessage smsMsg = SmsMessage.createFromPdu((byte[])objArr[i]);
String smsBody = smsMsg.getMessageBody();
String senderNumber = smsMsg.getDisplayOriginatingAddress();
sms+= "From: "+senderNumber+"\nContent: "+smsBody+"\n";
}
lbs.setText(sms);
}
Here's my Manifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gavadev.com.smsreceiver">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"></activity>
</application>
</manifest>
Use a Broadcast Receiver which will not be unregistered when destroying the activity:
public class MyReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
// your processing will go here...
}
}
}
Register the receiver via manifest file as following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiverpremier"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.broadcastreceiverpremier.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
Refactored my simple dial application and moved my custom 'ServiceReceiver' class so it is nested in my Main Class like so (partial code showing just nested ServiceReceiver class)
public class Main extends Activity implements OnClickListener{
public class ServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Log.d("foo","incomingNumber : "+incomingNumber);
EditText incomingNum = (EditText) findViewById(R.id.inputText);
incomingNum.setText(incomingNumber);
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
My question is, after doing this, what changes need to be made to the manifest.xml?
Here is my existing (old) manifest file when ServiceReceiver was in a seprate file, class, not nested.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.foo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CALL_PHONE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.foo.Main"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
Did you try something like this:
<receiver android:name="com.example.foo.Main$ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
and make the Receiver class as static
In my application I need to process physical button events such as volume button clicks. For that I'm using audiomanager's registerMediaButtonEventReceiver method. There's an article relevant to my situation, though I can't get it working.
Here's code I'm using:
public class VolumeBroadcastActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), RemoteControlReceiver.class.getName()));
}
public class RemoteControlReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(VolumeBroadcastActivity.this, "1",1).show();
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="localhost.volume.broadcast"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".VolumeBroadcastActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
</manifest>
Your RemoteControlReceiver class is an inner class of VolumeBroadcastActivity. Your manifest doesn't say so and I don't think it could. Make it a regular class and precede its android:name with a dot.
You might need this permission (declare in manifest):
uses-permission android:name="android.permission.BLUETOOTH"
I have a BroadcastReceiver declared in the following AndroidManifest.xml, but I never get the SMS_RECEIVED broadcast. Any ideas what is happening?
This is the BroadcastReceiver:
public class SMSReceiver1 extends BroadcastReceiver
{
private static final String LOG_TAG = "Thiri The Wut Yee";
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent)
{
Log.i(LOG_TAG, "Receive ");
Toast.makeText(context, "RECEIVED", Toast.LENGTH_LONG).show();
}
}
And the AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android_programmers_guide.SMSReceiver1"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".SMSReceiver1"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
Do you have this permission in your manifest?
<uses-permission android:name="android.permission.RECEIVE_SMS" />