1
我有一個應用程序,用戶可以通過應用程序向他人發送食譜。 現在,應用程序能夠發送通知,但是,當通知被點擊時,它不會打開預期的活動(SmsViewActivity),它只是關閉抽屜。通知不會打開活動
下面是SmsViewActivity代碼:
public class SmsViewActivity extends Activity {
private static final String TAG = "SmsViewActivity";
private static final String SMS_FOOD = "food_recieved";
private FoodJSONSerializer mSerializer;
public Button mSaveButton, mDismissButton;
public int mNotificationId;
public String message;
private EditText mTitleField;
private CheckBox mImperialCheckBox;
private CheckBox mMetricCheckBox;
private EditText mServingsField;
private EditText mDirectionsField;
private Spinner mMetricSpinner;
private Spinner mImperialSpinner;
Food mFood;
private String msg;
private Activity mActivity;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mActivity = this;
setContentView(R.layout.sms_view);
mSaveButton = (Button) findViewById(R.id.save_button_sms);
mDismissButton = (Button) findViewById(R.id.dismiss_button_sms);
this.msg = getIntent().getStringExtra("message");
try {
JSONObject jsonRecipe = new JSONObject(this.msg);
this.mFood = new Food(jsonRecipe);
Log.i(TAG, "Food = " + mFood);
} catch (JSONException e) {
e.printStackTrace();
}
mNotificationId = getIntent().getIntExtra("notificationid", 0);
if (mNotificationId == 0) {
Log.e(TAG, "Could not retrieve notification ID.");
Toast.makeText(this, "A fatal error has occurred in SMS viewer.",
Toast.LENGTH_LONG).show();
finish();
}
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationMgr = (NotificationManager) getSystemService(ns);
notificationMgr.cancel(mNotificationId);
this.mTitleField = (EditText) findViewById(R.id.food_title_sms);
this.mTitleField.setText(mFood.getTitle());
this.mServingsField = (EditText) findViewById(R.id.food_servings_sms);
this.mServingsField.setText(Integer.toString(mFood.getServings()));
this.mDirectionsField = (EditText) findViewById(R.id.directions_text_sms);
this.mDirectionsField.setText(mFood.getDirections());
this.mSaveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FoodStorage.get(mActivity).addFood(mFood);
finish();
}
});
this.mDismissButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
backToList();
finish();
}
});
}
public void backToList() {
Intent i = new Intent(this, FoodListFragment.class);
startActivity(i);
}
}
而且,這裏是接收器的活動:
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
Bundle bundle = intent.getExtras();
Map<String, String> msgTexts = new LinkedHashMap<String, String>();
if (bundle != null){
Object[] pdus = (Object[]) bundle.get("pdus");
for (Object pdu : pdus) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
String incomingMsg = message.getMessageBody();
String originatingNumber = message.getOriginatingAddress();
Log.i(TAG, "Rcvd: from " + originatingNumber + " Msg: " + incomingMsg);
msgTexts.put(originatingNumber, incomingMsg);
smsMessage = incomingMsg;
}
}
processMessages(msgTexts, mNextNotificationId);
}
public void processMessages(Map<String, String> msgTexts, int notificationId){
for (Entry<String, String> entry : msgTexts.entrySet()) {
Log.i(TAG, "Processing message: " + entry.getValue());
Intent notificationIntent = new Intent(mContext, SmsViewActivity.class);
notificationIntent.putExtra("message", entry.getValue());
notificationIntent.putExtra("sender", entry.getKey());
notificationIntent.putExtra("notificationid", notificationId);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Recipe Received")
.setContentTitle("Recipe Received")
.setContentIntent(resultPendingIntent)
.setContentText("from " + entry.getKey())
.setWhen(System.currentTimeMillis());
NotificationManager notificationMgr = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationMgr.notify(notificationId++, mBuilder.build());
}
}
謝謝!這使它工作!你能否向我解釋這會帶來怎樣的改變?再次感謝! – CoderClouds