【问题标题】:Android. Check checkbox in another activity when receive SMS安卓。收到短信时选中另一个活动中的复选框
【发布时间】:2013-04-03 05:20:39
【问题描述】:

(对不起我的英语) 我有一张带有 telit gt864-quad 调制解调器 GSM 的 Picdem.net2 开发卡。当连接到开发卡的东西出现问题(例如水控制器的问题)时,开发卡会向我的安卓手机发送一条短信,以阻止我。

当短信到来时,它应该显示一个通知,并选中另一个名为“etat”(状态)的活动中的“警报”复选框。 SMS 活动名为“SMSReceive”。

所以实际上我可以接收短信,它会带来一个警报通知,如果我点击它,谁会引导到应用程序。但我不知道如何检查复选框...

(CheckBox)findViewById(R.id. ...) 不起作用。

package com.example.locatiris;

import android.net.Uri;
import android.os.Bundle;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.content.res.Resources;
import android.support.v4.app.NotificationCompat;
import android.telephony.SmsMessage;
import android.widget.Toast;
import android.app.PendingIntent;
import android.widget.CheckBox;

public class SMSReceive extends BroadcastReceiver
{
CheckBox water = null;
private static final int Notification_ID = 1234;
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";


    public void onReceive(Context context, Intent intent)
    {


    Bundle bundle = intent.getExtras();
    //SMS pdus = format de SMS
    Object messages[] = (Object[]) bundle.get("pdus");
    SmsMessage smsMessage[] = new SmsMessage[messages.length];

    for(int i=0; i<smsMessage.length; i++)
    {
        smsMessage[i] = SmsMessage.createFromPdu((byte[])messages[i]);
        //récupérer le numéro
     String msg_from = smsMessage[i].getOriginatingAddress();
     //Récuperer message
     String msg_cont = smsMessage[i].getMessageBody();

        if(msg_from.equals("+33NUMBER") & msg_cont.contains("water"))
        {
            Toast toast = Toast.makeText(context,
            "Message reçu " + smsMessage[0].getMessageBody(),
            Toast.LENGTH_LONG);
            toast.show();
             //faire une action lors de réception
            createNotification(context);

        }                    
    }
}

}

这里是如何启动“etat”活动:

public class etat extends Activity// implements View.OnClickListener
{
EditText Chau = null;
EditText Vann = null;
EditText Serr = null;
EditText Digi = null;
Button Actu = null;

@Override
public void onCreate(Bundle savedInstanceState) 
{ ...

这里是通知创建者:

private final void createNotification(Context context2)
{
    //vibreur spécial
    long[] a = {0, 200, 200, 200, 500, 200, 500, 200, 200, 500, 200 };

    //creer notification et ses parametres
    NotificationCompat.Builder builder =  
            new NotificationCompat.Builder(context2)  
    .setSmallIcon(R.drawable.ic_launcher)
    .setTicker("Problème location détecté")
    .setWhen(System.currentTimeMillis())
    .setAutoCancel(true)
    .setVibrate(a)
    .setSound(Uri.parse("file:///sdcard/Music/ZOUZ2.mp3"))
    .setContentTitle("ALERTE Location")
    .setContentText("Un problème est survenu dans la location");

    //ouvrir l'application au clic sur notification
    Intent notificationIntent = new Intent(context2, MainActivity.class);  
    PendingIntent contentIntent = PendingIntent.getActivity(context2, 0, notificationIntent,   
            PendingIntent.FLAG_UPDATE_CURRENT);  
    builder.setContentIntent(contentIntent);  

    NotificationManager manager = (NotificationManager) context2.getSystemService(Context.NOTIFICATION_SERVICE);  
    manager.notify(Notification_ID, builder.build());  
}

感谢您的帮助!

【问题讨论】:

    标签: android android-activity checkbox sms


    【解决方案1】:

    在通知点击时使用以下代码来调用活动

    Intent intent = new Intent(this, etat.class);
    intent.putExtra("isSMSReceived", true/false);
    startactivity(intent)
    

    在您的活动中使用此代码

    Intent intent = getIntent();
    boolean state = intent.getBooleanExtra("isSMSReceived");
    
    CheckBox cb = (CheckBox)findViewById(R.id. ...);
    cb.setChecked(state);
    

    【讨论】:

    • 是的,我了解您的代码现在是如何工作的,但我会尝试纠正正在发生的错误
    • 我将通知创建器放在第一篇文章中。我的错误是:Intent(SMSReceive, class ) 未定义 并且 方法 startactivity(Intent) 未定义 SMSReceive 类型 对于其他活动:Intent 类型中的方法 getBooleanExtra(String, boolean) 不适用于参数 (String)跨度>
    • 我修改了代码,但问题是我的通知转到“登录”活动,而不是 etat 活动。所以我将第二个代码放入“登录”活动中,它没有说有错误,但应用程序在启动时强制关闭。另外,如果我必须在 getBooleanExtra("isSMSReceived",true) 方法中添加布尔值
    • 但无论如何感谢您的回答!看来我不能去有你的第二个代码的活动。我认为是 getIntent() 导致力关闭,但我不确定。在 logcat 中我可以看到: Caused by : java.lang.nullpointerexception
    • 所以,没有人知道可能是什么问题?谢谢!
    猜你喜欢
    • 2014-03-07
    • 2021-08-23
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多