通知在真实设备上不起作用,但在仿真器上工作

时间:2014-04-22 20:51:29

标签: android notifications sms emulation

我正在处理发送加密短信的应用。我用接收器制作它,所以当加密的SMS,公共密钥或加密的SMS密钥被接收时,调用通知。它在模拟器上运行得很好,但在我的手机上,它有相同的Android版本,没有通知。请问你知道问题出在哪里吗?我添加了与通知有关的所有代码。谢谢你的建议!

SMSReceiver.java:

public class SMSReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle=intent.getExtras();
    String sender="";
    String message="";
    SmsMessage[] msgs=null;
    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]);
            if(i==0){
                sender=msgs[i].getOriginatingAddress();
            }

            message=msgs[i].getMessageBody().toString();
        }
    }
    Intent i=new Intent();
    i.putExtra("type", message);
    i.putExtra("sender", sender);
    i.setAction("SMS_RECEIVED_ACTION");
    context.sendBroadcast(i);


}

 }

MainActivity.java:

 public class MainActivity extends FragmentActivity {

FragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
int Number = 0;


String phoneNumber;
String message;
IntentFilter intentFilter;
private static final int ID = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAdapter = new FragmentAdapter(getSupportFragmentManager());
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);


    intentFilter = new IntentFilter();
    intentFilter.addAction("SMS_RECEIVED_ACTION");


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}




@Override
protected void onResume() {
    super.onResume();
    registerReceiver(intentReceiver, intentFilter);
}

private BroadcastReceiver intentReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        phoneNumber = intent.getExtras().getString("sender");
        message = intent.getExtras().getString("type");
        getNotification();
    }
};

public void getNotification() {

    char key = message.charAt(0);
    String sentence = "";

    switch (key) {
    case '@':
        sentence = "Symmetric key received!";
        break;
    case '$':
        sentence = "Public key received!";
        break;
    case '£':
        sentence = "Encrypted SMS received!";
    default:
        sentence = "";
        break;
    }

    if (sentence.length() > 0) {
        final NotificationManager mgr = (NotificationManager)     this.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification note = new Notification(R.drawable.ic_launcher,     "Secure SMS notification!", System.currentTimeMillis());
        PendingIntent i = PendingIntent.getActivity(this, 0, new     Intent(this, MainActivity.class), 0);
        note.setLatestEventInfo(this, "Secure SMS notification!", sentence, i);
        mgr.notify(ID, note);
    }
}

 }

的AndroidManifest.xml:

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

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


<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/icon2"
    android:label="@string/app_name"

    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SMSListView"

        >
    </activity>
     <receiver 
        android:name=".SMSReceiver"      >

        <intent-filter >
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>
</application>

</manifest>

0 个答案:

没有答案