i know this question may be asked by another user ...but i can not solve my problem
i create one broadcast for receive sms and it worked well but when user clear ram broadcast does not work ...
how i can create a broadcast that work even user clear ram
it is my code
public class ReceiveSms extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("log","sms received");
// Toast.makeText(App.context,"you have sms",Toast.LENGTH_SHORT).show();
Object[] pdus= (Object[]) intent.getExtras().get("pdus");
SmsMessage sms=SmsMessage.createFromPdu((byte[]) pdus[0]);
String body=sms.getMessageBody();
String sender=sms.getDisplayOriginatingAddress();
Log.i("log","sms body"+body);
Toast.makeText(App.context,"message from :"+sender,Toast.LENGTH_SHORT).show();
Intent startProgram=new Intent(App.context,MainActivity.class);
startProgram.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
App.context.startActivity(startProgram);
}
}
and mainfast
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.masiha68.sms">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".RC"
android:process=":remote"
android:enabled="true"
>
</service>
<receiver android:name=".ReceiveSms"
android:process=":remote"
>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
In huawei device, you need to enable you app in 'Protected apps' to keep running after the screen off or after you kill app from background.
For that goto,
Phone Manager -> Power saving -> Protected apps -> find your app and 'enable' it.
Related
I've almost made my sms app, only one bug still remains. When phone receives incoming sms it somehow goes to my app and to default one. I thought that problem was in action of intent, but changing it does nothing. The question is - how to intercept incoming sms and forbid default app to do the same?
AndroidManifest:
<manifest package="lexzcq.com.smska"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission
android:name="android.permission.SET_PREFERRED_APPLICATIONS"
tools:ignore="ProtectedPermissions"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter android:priority="999">
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.APP_MESSAGING"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<receiver
android:name=".SmsReceiver"
android:permission="android.permission.BROADCAST_SMS"
>
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<category android:name="android.intent.category.APP_MESSAGING"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<activity android:name=".ChatWindow">
<intent-filter>
<action android:name="com.lexzcq.com.lexzcq.sms_receive_intent"/>
</intent-filter>
</activity>
<application android:name=".MyApp">
</application>
</application>
</manifest>
SmsReceiver.class
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive (Context context, Intent intent) {
*//here I handle sms from pdus*
Intent broadcastIntent = new Intent ()
.putExtra ("address", address)
.putExtra ("body", body)
.setAction ("com.lexzcq.sms_receive_intent");
*//here I'm writing sms to database*
context.sendBroadcast (broadcastIntent);
abortBroadcast ();
}
}
}
If needed - I can provide broadcast receivers from MainActivity.class
P.S. Most weird part - that when I'm sending sms it somehow saves to default app but I didn't broadcast it with SMS_RECEIVED_ACTION.
i am writing a broadcastreciever to listen for SMS_RECEIVED action .it works perfectly fine if i register it dynamically but if i register it statically in the Manifest file the broadcast receiver is not being called.
<receiver android:name=".MyReceiverBroadcast"><intent-filter>
<action android:name="android.provider.Telephony.SMS_RECIEVED"></action>
</intent-filter></receiver>
My broadcast reciever is as follows
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(action.equals(SMS_RECEIVED_ACTION))
{
Log.v("sms", "reciever called");
Object[] messages=(Object[])intent.getExtras().get("pdus");
for(Object message:messages)
{
byte[] messagedata=(byte[])message;
SmsMessage smsmessage=SmsMessage.createFromPdu(messagedata);
processmessage(smsmessage,context);
}
}
}
private void processmessage(SmsMessage smsmessage,Context c) {
String from=smsmessage.getOriginatingAddress();
String messcontent=smsmessage.getMessageBody();
Toast.makeText(c,"from ="+from+" and mess="+messcontent,Toast.LENGTH_LONG).show();
Log.v("sms","from ="+from+" and mess="+messcontent);
}
My Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tech.myfirst" >
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<activity
android:name=".SmsReceiverDemo"
android:label="SmsReciever Demo"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.tech.myfirst.MyReceiverBroadcast"><intent-filter>
<action android:name="android.provider.Telephony.SMS_RECIEVED"></action>
</intent-filter></receiver>
</application>
</manifest>
I have added all the necessary permissions.
What i am missing here.
i am running the program on emulator nexus 5 api 21.
thanks
Assuming everything else is correct, it appears the problem is that you've misspelled RECEIVED in the <intent-filter>'s <action> for the <receiver> entry in the manifest. It should be:
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
You might also want to check the value of the SMS_RECEIVED_ACTION String.
I've had this problem before, and was solved by changing the attribute
android:name=".MyReceiverBroadcast"
To
android:name="my.package.name.MyReceiverBroadcast"
I don't know why this is happening, but it worked for me.
I have two launcher activities,
1. Receiver activity which is a Broadcast Receiver.
2. An Activity which should be main Launcher activity.
When an SMS arrives the receiver activity launches the .MainActivity(BroadcastReceiver) and it further start one service. (Without GUI) and without opening Settings activity.
Another activity .Settings is Main launcher activity.
Goal I want to Achive:
When sms arrived receiver activity work as it is. But When I want to change the settings Setting activity starts.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.aa.FindLocation"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.aa.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.aa.Settings"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.aa.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MainActivity"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<service android:name=".Servc" >
<intent-filter>
<action android:name="com.example.Servc" />
</intent-filter>
</service>
</application>
</manifest>
Problem
1.
[2014-07-06 02:55:14 - Aa] Uploading Aa.apk onto device 'S5830f4524b76'
[2014-07-06 02:55:14 - Aa] Installing Aa.apk...
[2014-07-06 02:55:18 - Aa] Success!
[2014-07-06 02:55:18 - Aa] Starting activity com.example.aa.Settings on device S5830f4524b76
[2014-07-06 02:55:19 - Aa] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.aa/.Settings }
2.
What changes should be made in manifest to make it working?
Don't define your sms receiver in the manifest. Remove it.
Create Broadcast receiver that runs on boot up by defining the below element in your manifest :
<receiver android:name=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
You will need to add this permission to your manifest :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Then register your sms receiving BroadcastReceiver in there :
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
SmsReceiver smsreceiver = new SmsReceiver();
IntentFilter fltr_smsreceived = new IntentFilter(
"android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsreceiver, fltr_smsreceived);
}
}
SmsReceiver is your broadcast receiver. For example :
public class SmsReceiver extends BroadcastReceiver {
void setActivityHandler(){
//instantiate member variables if required
}
#Override
public void onReceive(Context context, Intent intent) {
Bundle pdusBundle = intent.getExtras();
Object[] pdus=(Object[])pdusBundle.get("pdus");
SmsMessage messages=SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.d("tag","message body : " + messages.getMessageBody());
}
}
I see this has been asked quite a bit, but I can't seem to resolve my problem with what is out there.
My onReceive() method in broadcast receiver isn't being called.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.app.test.TestActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
BootUpReceiver.java
package com.app.test;
public class BootUpReceiver extends BroadcastReceiver {
private static final String TAG = "TESTAPP_BootUpReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "helllllllllllllllo");
Toast.makeText(context, "boot completed received", Toast.LENGTH_LONG).show();
// Intent i = new Intent(context, TestActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(i);
}
}
Have tried using the entire path instead of .BootUpReceiver, didn't work. Not seeing anything from logcat or any Toast messages. Going into adb shell and emitting the boot_completed event that way doesn't help as the device then reboots.
Is there anything I am doing wrong? I read something about applications being inactive when device boots, does that affect my problem?
Here are some reference in Android developer website
http://developer.android.com/guide/topics/data/install-location.html
Broadcast Receivers listening for "boot completed"
The system delivers the ACTION_BOOT_COMPLETED broadcast before the external storage is mounted to the device. If your application is installed on the external storage, it can never receive this broadcast.
So I am trying to develop a custom lockscreen
but my broadcastreceiver won't fire
my manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.alexander.fuchs.lockscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".app"
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.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
my receiver :
public class myreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("receiver","works");
Toast.makeText(context,"works",Toast.LENGTH_LONG).show();
Intent in=new Intent(context,app.class);
context.startActivity(in);
}
}
the receiver should show me that it is fired :D
but ther aren't any logs in logcat
well , for the screen off and screen on , this cannot be inside the manifest , but only at runtime . see this:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
for the bootup , it must be in the manifest , so something else is wrong with it.check the path of the class. this is surely the cause of the problem.