利用广播接收器的Andr​​oid阻止接收短信 [英] Android Block Incoming SMS using BroadCastReceiver

查看:142
本文介绍了利用广播接收器的Andr​​oid阻止接收短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只要我的应用程序正在运行阻止传入的短信。我能做到这一点,但问题是在我关闭应用程序,甚至重新启动,甚至将其卸载,用户将无法收到短信了。所以,我怎样才能让阻止,只有当它的运行接收到的SMS应用程序,当它被关闭或卸载等停止拦截短信。这里是我的code:

I want to block incoming SMS message as long as my application is running . I could achieve that but the problem is after I close the app or even restart or even uninstall it the user will not be able to receive SMS message anymore . So How can I make the application to block incoming SMS only when it's running and when it gets closed or uninstalled etc.. to stop blocking SMS. Here's my code :

BroadCastReceiver.Java

BroadCastReceiver.Java

package com.example.sms;
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 
{

    public void onReceive(Context context, Intent intent)
    {
     abortBroadcast();
     }
    }

MainActivity.java

MainActivity.java

package com.example.sms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Manifiest:

Manifiest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
<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" >
        <receiver android:name=".BroadCastReceiver">
    <intent-filter android:priority="2147483647">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

        <activity
            android:name="com.example.sms.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>
    </application>

</manifest>

请注意:我已经在我的Andr​​oid模拟器已发送使用Telnet短信测试了Android 2.2的应用

Note: I've tested the application on android 2.2 on my android emulator "Sent SMS messages using Telnet"

推荐答案

检查您的应用程序正在运行则仅中止广播接收机别的不

Check if your app is running then only abort broadcast receiver else not

//define this variable above onReceive() with default value as false;
boolean appRunningInBack=false;

ActivityManager am = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();

if(packageName.equalIgnoreCase("your app package name") || appRunningInBack)
{    
    appRunningInBack=true;
    abortBroadCast();
} else {

}

此外,在添加权限的Andr​​oidManifest.xml

<uses-permission android:name="android.permission.GET_TASKS" />

这之后,当你的应用程序中去,而background.or运行。这个变量将是真实的。

After this when your app goes in background.or while running .this variable will be true.

这篇关于利用广播接收器的Andr​​oid阻止接收短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆