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.
Related
I'm pretty new to android programming. I'm trying to start a service when the phone is started but it does not work. I've already seen other question done by other users but no one worked till now. This is my broadcast receiver.
public class StartBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent intent1 = new Intent(context,MyService.class);
context.startService(intent1);
}
}
}
and this is my manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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>
<receiver android:name=".StartBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
</application>
In the log i read this
W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x9000010 (has extras) } to com.google.android.apps.docs/.app.NotificationChannelReceiver requires android.permission.RECEIVE_BOOT_COMPLETED due to sender null (uid 1000)
Thank you in advance for the answers
Remove
<category android:name="android.intent.category.DEFAULT"/>
from the <intent-filter> inside <receiver> tag. This category is only needed for <intent-filter> inside of <activity> declaration. For a <receiver> you only need the ACTION.
The error message you posted has nothing to do with your application, it is complaining about another application.
I want to receive broadcast BATTERY_LOW, so I declared broadcast receiver in manifest (in order not to rely on current activity. I want my application receive this broadcast even when it's not running. Here's what I did.
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".BatteryLevelReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.POWER_CONNECTED"/>
<action android:name="android.intent.action.POWER_DISCONNECTED"/>A
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
And BatteryLevelReceiver itself (package com.wayruha.serviceguard.onduty) :
public class BatteryLevelReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.w(this.getClass().getName(),intent.getAction());
Log.w(this.getClass().getName(),"Low level:"+intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1));
}
}
I cant receive any of declared events!There are some similar questions here, but none of them helps me. Im using telnet to set capacity or 'ac on/off' for an emulator.
Try to add permission in your manifest file:
<uses-permission android:name="android.permission.BATTERY_STATS"/>
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.
This my Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.testapp"
android:versionCode="1"
android:versionName="Pm61" >
<uses-sdk android:minSdkVersion="15"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<supports-screens android:anyDensity="true" />
<application android:label="#string/app_name" android:debuggable="true" android:largeHeap="true">
<activity android:name="com.abc.testapp.MainClass" android:label="#string/app_name" android:theme="#android:style/Theme.Translucent" android:hardwareAccelerated="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
.
.
.
.
.
.
<activity android:name="com.abc.testapp.BootLoad"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>
<activity android:name="com.abc.testapp.Rxmain" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" android:launchMode="singleTask">
</activity>
<receiver android:name="com.abc.testapp.MyReceiver" android:enabled="true">
<intent-filter android:priority="500">
<action android:name= "android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>
</application>
</manifest>
This is my MyReceiver class for broadcasting
package com.abc.testapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(Intent.ACTION_MEDIA_MOUNTED.equals(action))
{
Log.d("MYReceiver","Mounting Successfull");
Intent serviceActivity = new Intent(context, Rxmain.class);
serviceActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceActivity);
}
if(Intent.ACTION_BOOT_COMPLETED.equals(action))
{
Log.d("MYReceiver","Boot Successfull");
Intent serviceActivity = new Intent(context, BootLoad.class);
serviceActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceActivity);
}
}
}
My device is stand alone device, only my app will come on boot.
I want when boot complete it shall launch BootLoad activity and after MEDIA_MOUNTED it should launch Rxmain Activity.
But my bootLoad activity is not coming
So I have some doubts in this:
It is working sometimes but sometimes not?
2.what is this Priority in Intent-filter?
what this data scheme does?
what I am doing is correct or not?
please suggest me
The problem is the way you've defined the intent filter for the broadcast receiver. This is your definition:
<receiver android:name="com.abc.testapp.MyReceiver" android:enabled="true">
<intent-filter android:priority="500">
<action android:name= "android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>
You've defined a single intent filter that will be triggered if the ACTION is either BOOT_COMPLETED or MEDIA_MOUNTED. But, by specifying the <data> tag, you will only receive broadcast Intents that have data with scheme=file.
The BOOT_COMPLETED Intent doesn't have any data, so your receiver won't get it.
You need to specify 2 separate intent filters, like this:
<receiver android:name="com.abc.testapp.MyReceiver" android:enabled="true">
<intent-filter android:priority="500">
<action android:name= "android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter android:priority="500">
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>
i am working on an app that will automatically decline incoming calls from specific numbers and send an sms in return.my broadcast receiver should be enabled from my app only.
so, i added ( android:enabled="false" ) this. but, the receiver is still on by default. i'm struck here.
<uses-permission android:name="android.permission.CALL_PHONE"/>
<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.sifydy.automsgr.Automsgr"
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.sifydy.automsgr.Receiver"
android:label="#string/app_name"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
wen i switch on my mobile, my receiver is also activated wen ever there is an incoming call.
i am able disable it from my app. everything is fine once i am in my app. how to disble it by default?