2012-02-25 50 views
0

所以,我想创建一个应用程序,可以给它的用户一个notif每次短信来。 我的主要问题是我混淆了如何结合我的2个Acitivites /类。我在SMSReceiver类中扩展BroadcastReceiver,以便它可以检测是否有任何新的SMS来到,并且我在我的SMSNotif类中扩展了Activity。问题是我在每个活动中都无法扩展超过1个CLASS。结合2扩展活动的短信通知:D

这是SMSReceiver类:

public class SMSReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context arg0, Intent arg1) { 
    // TODO Auto-generated method stub 

    Bundle bundle = arg1.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"; 
     } 

     Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show(); 
    } 
    //Intent i = new Intent(SMSReceiver.this, SMSNotif.class); 
} 

}

这是我SMSNotif类:

public class SMSNotif extends Activity{ 
static final int HELLO_ID = 1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    //String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    //int icon = R.drawable.ic_launcher; 
    String tickerText = "Hello"; 
    //long when = System.currentTimeMillis(); 

    Notification notification = new Notification(R.drawable.ic_launcher, tickerText, System.currentTimeMillis()); 

    //Context context = getApplicationContext(); 
    String contentTitle = "My notification"; 
    String contentText = "Hello World!"; 
    Intent notificationIntent = new Intent(this, SMSNotif.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); 
    notification.defaults = Notification.DEFAULT_ALL; 
    mNotificationManager.notify(HELLO_ID, notification); 

} 

}

同样,我的主要问题是:如何结合这些活动,所以我每次用户得到一个新的短信,我的应用程序将显示它notif(不是绝对的t面包形式SMSNotif)。

+0

您好所有注册的接收器! :D 对不起,我忘了说“你好” 该系统不允许我编辑我的帖子:( – 2012-02-25 16:42:58

+0

是不是更容易在'SMSReceiver'中编写通知代码? – Jin35 2012-02-25 17:19:22

+0

是的,先生,这就是我的意思。 。但我不知道如何结合它..:D 谢谢he2 – 2012-02-26 10:03:53

回答

0

这里是来解决问题的诀窍:

充分利用广播接收器级本地的活动类。通过这种方式,接收器可以做一些活动

public class SMSNotif extends Activity{ 

    class SMSReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      ... 
      someActivityMetod("Hello world from receiver"); 
     } 

    } 

    void someActivityMetod(String message) 
    { 
     ... 
    } 
} 

你也有(未)在活动代码

public class SMSNotif extends Activity{ 

      BroadcastReceiver myReceiver = null; 



    @Override 
    public void onPause() 
    { 

     if (myReceiver != null) 
     { 
      unregisterReceiver(myReceiver); 
      myReceiver = null; 
     } 
     super.onPause(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     if (myReceiver == null) 
     { 
      myReceiver = new SMSReceiver(); 
      IntentFilter filter = new IntentFilter("Some.Action.Code"); 
      registerReceiver(myReceiver, filter); 
     } 
    } 
+0

非常感谢! 顺便说一句,我弄糊涂在你的第二个代码.. 你使用onPause和onResume,但onCreate?在onResume之后? 对不起,我是一个初学者,我从来没有像这样使用嵌套类:D – 2012-02-26 15:10:08

+0

顺便说一句,有没有比创建一个像这样的嵌套类更好的方法? 非常感谢! :D – 2012-02-26 15:13:56

+0

你好,你还是需要onOnCreate。 onPause和onResume是必要的,因为活动只应在运行时才会收到并且可见 – k3b 2012-02-27 08:30:14