How to kill android application using android code? - android

I am develoing small android application in eclipse. In that project i kill the running process in android, i got the Permission Denial error. how can i solve this problem in android.
Anybody help for this problem....
THIS IS MY CODE
package com.example.nuts;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;
import android.*;
public class killprocess extends Activity
{
SmsManager smsManager = SmsManager.getDefault();
Recivesms rms=new Recivesms();
String Number="";
int pid=0;
String appname="";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try
{
Number=Recivesms.senderNum;
pid=Integer.parseInt(Recivesms.struid);
appname=getAppName(pid);
Toast.makeText(getBaseContext(),"App Name is "+appname, Toast.LENGTH_LONG).show();
ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = am.getRunningAppProcesses();
if (processes != null){
for (int i=0; i<processes.size(); i++){
RunningAppProcessInfo temp = processes.get(i);
String pName = temp.processName;
if (pName.equals(appname))
{
Toast.makeText(getBaseContext(),"App Name is matched "+appname+" "+pName, Toast.LENGTH_LONG).show();
int pid1 = android.os.Process.getUidForName(pName);
//android.os.Process.killProcess(pid1);
am.killBackgroundProcesses(pName);
Toast.makeText(getBaseContext(), "Killed successfully....", Toast.LENGTH_LONG).show();
}
}
}
smsManager.sendTextMessage(Number, null,"Your process Successfully killed..." , null,null);
}catch(Exception e)
{
Toast.makeText(getBaseContext(),e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private String getAppName(int Pid)
{
String processName = "";
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext())
{
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
if(info.pid == Pid)
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
//Log.d("Process", "Id: "+ info.pid +" ProcessName: "+ info.processName +" Label: "+c.toString());
//processName = c.toString();
processName = info.processName;
}
}
catch(Exception e)
{
//Log.d("Process", "Error>> :"+ e.toString());
}
}
return processName;
}
}
After executing the code. i got the following error...
Permission Denial:
killBackgroundProcess() from pid=894,
uid=10052 requires
android.permission.KILL_BACKGROUND_PROCESSES
Also i put the following line on manifest file
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESS" />
Anybody help for how to solve this problem...
Thanking you....
This is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nuts"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESS" ></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/clip1"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.nuts.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>
<activity
android:name="com.example.nuts.createpassword"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.nuts.editpassword"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.nuts.processList"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.nuts.killprocess"
android:label="#string/app_name"
>
</activity>
<receiver android:name=".Recivesms">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name="com.example.nuts.sensSms"
android:label="#string/app_name" >
</activity>
</application>

add permission in your manifest file
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"></uses-permission>
</manifest>

Goto your manifest file and add this
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

You need to add the relevant permission in your AndroidManifest.xml
From the official documentation :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.app.myapp" >
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
...
</manifest>

Add this permission to your manifest file to kill backgroud process
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
Note: If you are getting all the running application on your android then you can kill only manually installed application like subway surfer but you can't kill inbuilt android application like launcher of android.

Related

How to receive incoming SMS on Android KitKat or above?

I know from KitKat onward we need to make out app as default SMS app to be able to modify the SMS database.
But I don't want that. I only need to get notification of incoming SMS and display as toasts, and also send SMS.
Do I need to make my app default for that?
Edit 1:
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rtrgroup.sms_to_neta_server"
android:installLocation="preferExternal">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginScreen" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"/>
<activity android:name=".MainScreen" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
</activity>
<service android:name=".MainService"
android:exported="true">
</service>
<receiver android:name=".MainReceiver"
android:exported="true"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is my BroadcastReceiver:
package com.rtrgroup.sms_to_neta_server;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.widget.Toast;
import android.telephony.*;
public class MainReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage;
if (Build.VERSION.SDK_INT >= 19) { //For ver. >= KitKat
SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
currentMessage = msgs[0];
} else {
Object pdus[] = (Object[]) bundle.get("pdus");
currentMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
}
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String messageB1 = currentMessage.getDisplayMessageBody();
String msg = currentMessage.getMessageBody();
String address = currentMessage.getOriginatingAddress();
Toast.makeText(context, senderNum + ": " + msg, Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
}
}
}
}
You don't need to be the default SMS app to send SMS. SmsManager.sendTextMessage() still works as long as you have the android.permission.SEND_SMS permission.
To receive SMSs, you can monitor the android.provider.Telephony.SMS_RECEIVED broadcast, with android.permission.RECEIVE_SMS permission.
Ref. http://android-developers.blogspot.fi/2013/10/getting-your-sms-apps-ready-for-kitkat.html

Android SmsManager Don't send sms to the specific number

I am trying to make an android application which will send an sms when the device first boot up and after sending the sms successfully the application will be destroyed automatically.If the application fails to send the sms then it will try within 30 minutes from the device boot up time again.
But for the test purpose I am trying to send the sms to specific number.but it don't send the sms from the device. here is manifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.farhadrakib.waltonmobile.wst"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.NONE" />
</intent-filter>
</receiver>
</application>
</manifest>
and here is the broadcastReceiver code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MainActivity extends BroadcastReceiver{
private static final String DestinationAddress = "+8801746433855";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String DeviceId = telephonyManager.getDeviceId();
Log.d("DEVICEID",DeviceId);
//String Line1Number = telephonyManager.getLine1Number();
if (DeviceId != null){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(DestinationAddress, null, "My id: "+DeviceId, null, null);
}
}
}
can anyone help??I really need some good sugesstion

Get all BTS tower when receive an SMS

I can get bts info in one activity , but when use that code in OnRecive of this code it is not work , Why this code can not get bts tower?
package com.example.testneighbore;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import java.util.List;
public class SMS extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Toast.makeText(context, " START ", Toast.LENGTH_LONG).show();
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
List<NeighboringCellInfo> NeighboringList = telephonyManager.getNeighboringCellInfo();
String retStr="";
for (int i = 0; i < NeighboringList.size(); i++) {
int cid = NeighboringList.get(i).getCid();
int lac = NeighboringList.get(i).getLac();
retStr += "&" + cid + "," + lac + ",";
}
Toast.makeText(context,"Print: "+ retStr, Toast.LENGTH_LONG).show();
Toast.makeText(context, " END ", Toast.LENGTH_LONG).show();
}
}
}
My AndroidManifest.xml is :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testneighbore"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.testneighbore.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=".SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES"/>
</manifest>
I think my main problem is with Context, but I'm not sure.
your problem is related to context, because the context is involved with receiving sms. To solve this problem, you have to make thread and get bts info in it. it is better that your thread to be call within ~20 seconds delay . you can use omething like this in OnRecieve()
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// get info
}
}, 20000);

broadcast receivers to check internet connection

I am trying to use broadcast receivers to check if there is internet or not , I dont get an error but my application is not working , it doesnt inform about the existence of the internet .. Could anybody help ? here are my codes ... Thanks in advance
package com.example.internetconnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE );
if ( activeNetInfo != null )
{
Toast.makeText( context, "Active Network Type : "+ activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
if( mobNetInfo != null )
{
Toast.makeText( context, "Mobile Network Type : "+ mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
}
};
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.internetconnection"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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="com.example.internetconnection"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
activity_main.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.internetconnection"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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="com.example.internetconnection"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
You have registered it dynamically , then why you are using receiver tag in manifest. I think you are messing up the code .

How to block an incoming message in android?

I am trying to develop an app in android which blocks an incoming sms. I have set the priority but its not blocking the incoming sms. I have used this.abortBroadcast() also but no result.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
#SuppressWarnings("deprecation")
public class SmsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
this.abortBroadcast();
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
}
}
and the manifest file is like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="BVB.EDU"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SMS"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<receiver android:name=".SMSReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>
Here's what I use for blocking incoming texts.
SmsReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class BroadCastReceiver extends BroadcastReceiver
{
/** Called when the activity is first created. */
private static final String ACTION = "android.provider.Telephony.SEND_SMS";
public static int MSG_TPE=0;
public void onReceive(Context context, Intent intent)
{
String MSG_TYPE=intent.getAction();
if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED"))
{
// Toast toast = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
// toast.show();
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
{
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
// show first message
Toast toast = Toast.makeText(context,"BLOCKED Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for(int i=0;i<8;i++)
{
System.out.println("Blocking SMS **********************");
}
}
else if(MSG_TYPE.equals("android.provider.Telephony.SEND_SMS"))
{
Toast toast = Toast.makeText(context,"SMS SENT: "+MSG_TYPE , Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for(int i=0;i<8;i++)
{
System.out.println("Blocking SMS **********************");
}
}
else
{
Toast toast = Toast.makeText(context,"SIN ELSE: "+MSG_TYPE , Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for(int i=0;i<8;i++)
{
System.out.println("Blocking SMS **********************");
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="APP.PACKAGE.NAMEHERE"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true" />
<uses-feature android:name="android.hardware.telephony" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".APPACTIVITYHERE"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden" >
<service android:name=".MyService" android:enabled="true"/>
<receiver android:name="SmsReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_SENT"/>
</intent-filter>
</receiver>
<service android:name=".MyServiceSentReceived" android:enabled="true"/>
<receiver android:name="SmsReceiver">
<intent-filter android:priority="2147483645">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
The main thing to take away from the manifest is the service block, receiver block, and the permissions.
Add abortBroadcast(); in the if(bundle!=null){} block. that should stop it going to other apps. And I noticed that your Broadcast Receiver's name is SmsReceiver, but in Manifest, you gave it ".SMSReceiver" (case sensitive).
Problem is there in your manifest, you're closing <application> tag before the receiver tag and it's wrong. All components should be inside an application tag.
Your class name is SmsReceiver, and in manifest you declared as SMSReceiver,
so you won't get the broadcast at all for your receiver.
Use have to change your class name in manifest like below
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
And in your receiver, it's better to check the intent object for whether you got the message or not and then you can abort it.
But be careful it will abort all messages. If you want abort messages depending on some particular string you can do some manipulation on message what you got and then you can abort it.
If you want to abort all messages you can directly call abortBroadcast() before doing any manipulation on that message.
Bundle extras = intent.getExtras();
if ( extras != null )
{
// do you manipulation on String then if you can abort.
if(somecondition){
abortBroadcast();
}
}
Here is my manifest which working fine, once compare with your code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<application android:icon="#drawable/icon" android:enabled="true" android:label="#string/app_name">
<service android:name="com.mypackage.service.MyService" android:exported="true">
</service>
<receiver android:name="com.mypackage.receiver.MyReceiver">
<intent-filter android:priority="100"><action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter>
</receiver>
</application>
</manifest>
and the java code
public void onReceive( Context context, Intent intent )
{
if(intent != null){
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if ( extras != null ){
//read sms
}
}
}
}
#sankar
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="BVB.EDU"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SMS"
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=".SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>
Because of the security policy in Android 4.4 and up
to block the incoming SMS-messages it is required to give to the
application the permissions of "Default SMS-application"

Categories

Resources