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.
Related
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>
I know there are similar question in statckoverflow, but they just do NOT work for me.
Broadcast receiver(staticly registe via manifest.xml) can NOT receive package_remove event after installing on device (without running main activity)
But the receiver works if main activity is running.
To register broadcastreceiver staticly in AndroidManifest.xml as followings
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceivertesting"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.broadcastreceivertesting.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="com.example.broadcastreceivertesting.PackageBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
</manifest>
PackageBroadcastReceiver as a receiver are like following:
public class PackageBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
final String action = intent.getAction();
if (Intent.ACTION_PACKAGE_REMOVED.equals(action))
{
File file = new File("/storage/sdcard0/zzz/yyy");
if (file.exists())
{
file.delete();
}
boolean createDir = new File("/storage/sdcard0/zzz/").mkdirs();
Log.d("XXX", "XXXX createDir=" + createDir);
try
{
file.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Do I miss something ?
Try changing this...
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
to this...
<intent-filter>
<action android:value="android.intent.action.PACKAGE_REMOVED" />
<scheme android:value="package" />
For more information, see this link on receiving package broadcast intents...
https://groups.google.com/forum/#!topic/android-developers/aX5-fMbdPR8
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
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" />
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').