我想在新的特定短信出現時顯示從我的應用生成的通知。 所以通知生成方法是我的主要活動。當新的收入消息到來時,我必須生成通知。 這是我的BroadcastReceiver類。調用BroadcastReceiver中的主要活動方法
public class BroadCastReceiver extends BroadcastReceiver {//私有AudioManager myAudioManager;
public MainActivity mainactivity = null;
public BroadCastReceiver() {
}
public void setMainactivityHandler(MainActivity mainn){
this.mainactivity = mainn;
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Toast.makeText(context, "SMS_RECEIVED", Toast.LENGTH_LONG).show();
System.out.println("received sms");
Bundle bundle = intent.getExtras();
if (bundle != null) {
Log.i("cs.fsu", "smsReceiver : Reading Bundle");
Object[] pdus = (Object[])bundle.get("pdus");
SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[0]);
if(sms.getMessageBody().contains("aa")) {
abortBroadcast();
System.out.println("received correct");
Log.e("tag", "corecttttttttttttttttttttttttttttttttttttttttttt");
Toast toast = Toast.makeText(context, "BLOCKED Received SMS: ", Toast.LENGTH_LONG);
toast.show();
mainactivity.showNotification();
abortBroadcast();
}}
}
}
}
這是我的主要活動
public class MainActivity extends Activity {
public BroadCastReceiver Br = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Br = new BroadCastReceiver();
Br.setMainactivityHandler(this);
IntentFilter callInterceptorIntentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(Br, callInterceptorIntentFilter);
// listener handler
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnShowNotification:
showNotification();
break;
case R.id.btnCancelNotification:
cancelNotification(0);
break;
}
}
};
// we will set the listeners
findViewById(R.id.btnShowNotification).setOnClickListener(handler);
findViewById(R.id.btnCancelNotification).setOnClickListener(handler);
}
public void showNotification(){
Log.e("","show notification");
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(MainActivity.this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
// this is it, we'll build the notification!
// in the addAction method, if you don't want any icon, just set the first param to 0
Notification mNotification = new Notification.Builder(this)
.setContentTitle("New Post!")
.setContentText("Here's an awesome update for you!")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setSound(soundUri)
.addAction(R.drawable.ic_launcher, "View", pIntent)
.addAction(0, "Remind", pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// If you want to hide the notification after it was selected, do the code below
// myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}
public void cancelNotification(int notificationId){
if (Context.NOTIFICATION_SERVICE!=null) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
nMgr.cancel(notificationId);
}
}
}
當新的消息來它給這個錯誤
07-29 22:06:59.227 23021-23021/com.example.siluni.myapplication E/tag﹕ corecttttttttttttttttttttttttttttttttttttttttttt
07-29 22:06:59.267 23021-23021/com.example.siluni.myapplication d/AndroidRuntime:關閉VM 07-29 22:06:59.277 23021-23021/com。示例。 siluni.myapplication E/AndroidRuntime:致命例外:main 進程:com.example.siluni.myapplication,PID:23021 java.lang.RuntimeException:無法啓動接收方com.example.siluni.myapplication.BroadCastReceiver:java.lang。 NullPointerException:嘗試調用空對象的虛方法'void com.example.siluni.myapplication.MainActivity.showNotification()'參考
謝謝先生,我現在就試試吧 –
@SiluniUpeksha,歡迎你。如果你發現它有用,然後善意upvote,如果它的工作,那麼請接受它。謝謝 –
是的,我會先生。 –