BroadcastReceiver的时候没有发射器是present,在奇巧奇巧、发射器、时候、BroadcastReceiver

2023-09-04 03:01:09 作者:箫江落舞

我在至极,我必须创建具有以下属性的应用程序的项目:

没有LAUNCHER :类机器人:名称=android.intent.category.LAUNCHER 必须读短信从特定号码发送的,当它,推出一个活动 在大多数Android版本的工作(我现在的目标是从 [升级Froyo,2.2]至[奇巧,4.4] )

到目前为止,我的问题是,在奇巧,我BoradcastReceiver不只是安装应用程序时,如果应用程序被运行一次工作,但是,那么它执行正确。在以前的版本中,它的行为正确。 我已阅读,在版本[蜂巢3.1]有变化广播系统,这个问题例如,说明我的问题是知道的。

我的问题是:有没有办法安装一个应用程序,并保持沉默,直到需要。比如他们的方式上低于[蜂窝]版本的工作?

如果是这样,可有人点了方向。 如果不是,将在引导过程中启动应用程序一次,然后关闭这将是一个合理的方法? 在任何情况下:我建立什么感觉错了,因为在为我做的认为不好的做法?我的系统需要用户主动安装的APP,以及APP的具有触发附加的声音/消息时给出的运动,以指示该用户是在物理危险的目的。 该系统用于indicdate该用户,他的家中,或某些他/她的属性的是在直接危险,所以它必须醒来,并通知他/她的下最circunstances

清单:

 < XML版本=1.0编码=UTF-8&GT?;
 

 <  - <使用-SDK安卓的minSdkVersion =8/> - >

<使用-权限的Andr​​oid:名称=android.permission.RECEIVE_SMS/>
<使用-权限的Andr​​oid:名称=android.permission.READ_SMS/>
<使用-权限的Andr​​oid:名称=android.permission.VIBRATE/>
<使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/>
<应用
    机器人:allowBackup =真
    机器人:图标=@可绘制/ ic_iconedesktop
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>

    <活动
        机器人:名称=。MainActivity
        机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>
            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;

    <接收机器人:SMSbroadcastReceptorNAME =>
        <意向滤光器>
            <作用机器人:名称=android.provider.Telephony.SMS_RECEIVED/>
        &所述; /意图滤光器>
    < /接收器>

< /用途>
 

接收器

 进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.telephony.SmsManager;
进口android.telephony.SmsMessage;
进口android.util.Log;
进口android.widget.Toast;

公共类SMSbroadcastReceptor扩展的BroadcastReceiver {
最后SmsManager的短信= SmsManager.getDefault();
公共无效的onReceive(上下文contexto,意图int​​encao){
    最后的捆绑包= intencao.getExtras();
    尝试 {
        如果(捆绑!= NULL){
            最终目标[] pdusObj =(对象[])bundle.get(的PDU);
            截止日的String =;
            字符串mensagem =;
            的for(int i = 0; I< pdusObj.length;我++){
                SmsMessage smsRecebido = SmsMessage.createFromPdu((字节[])pdusObj [I]);
                截止日= smsRecebido.getDisplayOriginatingAddress();
                mensagem = smsRecebido.getDisplayMessageBody();
                Log.i(SMSbroadcastReceptor,截止日:截止日+ +\ N,O阙:+ mensagem);
                吐司面包= Toast.makeText(contexto,截止日:截止日+ +\ N,O阙:+ mensagem,Toast.LENGTH_LONG);
                toast.show();
            }
            如果(quem.equals(+内心潜在号在这里)){//
                // abortBroadcast();
                意图comecarAMain =新的意图(contexto,MainActivity.class);
                comecarAMain.putExtra(我的包,命令);
                comecarAMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                contexto.startActivity(comecarAMain);
            }
        }
    }赶上(例外五){
        Log.e(SMSbroadcastReceptor,Excecao SMSbroadcastReceptor+ E);
    }
}
 
broadcastReceiver

}

解决方案   

有没有办法安装一个应用程序,并保持沉默,直到需要

不是真的。东西已经用明确的意图来任何明显的登记接收器将工作之前启动你的应用程序的组件之一。如果你不会有一个主屏幕发射器,我不知道还有什么会启动您的应用程序的组件之一,使用一个明确的意图

  

将开机时启动应用程序一次,然后关闭这将是一个合理的方法?

没有,因为你没有办法让在启动时控制,除了通过清单注册的BroadcastReceiver ,这使你的,你现在是同一位置。

  

我的系统需要主动安装APP的用户

然后有应在具有发射器的活动,至少一个一次性发射没有特别的问题。欢迎您,然后关闭该活动,但如果用户强制停止您的应用程序,你会再次处于停止状态,不再响应广播。

  

和APP的具有触发附加的声音/运动的信息被赋予时,以指示该用户是在物理危险的目的

那么就应该在刚离开发射活动单独没有问题。毕竟,用户需要能够配置您的应用程序的行为。

I have a project in wich I must create an application with the following properties:

No LAUNCHER: category android:name="android.intent.category.LAUNCHER" It must read SMSs sent from an specific number, and when it does, launch an Activity Work on most Android versions (my target now is from [Froyo, 2.2] to [Kitkat, 4.4])

So far, my issue is that, on Kitkat, my BoradcastReceiver does not work when the app is just installed, however, if the app is run once, then it performs correctly. On prior versions, it behaves correctly. I have read that on version [HoneyComb 3.1] there were changes in the broadcast system, this question for instance, shows that my issue is know.

My question is: Is there a way to install an APP, and keep it silent, until needed. Such as the way they work on versions below [Honeycomb]?

If so, can someone point a direction. if not, would starting the app once during boot, then closing it would be a reasonable approach? In any case: What I am building "feels" wrong, as in is what I am doing considered bad practice? My system needs the user to actively install the APP, and the APP has the purpose of triggering additional sounds/movement when a message is given, to indicate that the user is in physical danger. The system is used to indicdate that the user, his home, or some of his/hers property is in direct danger, so it must awake and notify him/her under most circunstances.

MANIFEST:

<?xml version="1.0" encoding="utf-8"?>

<!--<uses-sdk android:minSdkVersion="8" /> -->

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_iconedesktop"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <activity
        android:name=".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=".SMSbroadcastReceptor">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

</application>

RECEIVER

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SMSbroadcastReceptor extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context contexto, Intent intencao) {
    final Bundle bundle = intencao.getExtras();
    try {
        if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            String quem = "";
            String mensagem = "";
            for (int i = 0; i < pdusObj.length; i++) {
                SmsMessage smsRecebido = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                quem = smsRecebido.getDisplayOriginatingAddress();
                mensagem = smsRecebido.getDisplayMessageBody();
                Log.i("SMSbroadcastReceptor", "Quem: " + quem + "\n, O que: " + mensagem);
                Toast toast = Toast.makeText(contexto, "Quem: " + quem + "\n, O que: " + mensagem, Toast.LENGTH_LONG);
                toast.show();
            }
            if (quem.equals("+MY HIDDEN NUMBER IS HERE")) {// 
                // abortBroadcast();
                Intent comecarAMain = new Intent(contexto, MainActivity.class);
                comecarAMain.putExtra("MY PACKAGE", "A COMMAND");
                comecarAMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                contexto.startActivity(comecarAMain);
            }
        }
    } catch (Exception e) {
        Log.e("SMSbroadcastReceptor", "Excecao SMSbroadcastReceptor" + e);
    }
}

}

解决方案

Is there a way to install an APP, and keep it silent, until needed

Not really. Something has to use an explicit Intent to start up one of your app components before any manifest-registered receivers will work. If you will not have a home screen launcher, I do not know what else would start up one of your app components using an explicit Intent.

would starting the app once during boot, then closing it would be a reasonable approach?

No, because you have no way to get control at boot time, except via a manifest-registered BroadcastReceiver, which puts you in the same position that you are now.

My system needs the user to actively install the APP

Then there should be no particular problem in having a launcher activity, at least for a one-time launch. You are welcome to then disable that activity, though if the user force-stops your app, you will once again be in the stopped state and will no longer respond to broadcasts.

and the APP has the purpose of triggering additional sounds/movement when a message is given, to indicate that the user is in physical danger

Then there should be no problem in just leaving the launcher activity alone. After all, the user needs to be able to configure the behavior of your app.