2013-10-01 99 views
0

這裏我想打開一個名爲View的新活動類。一旦點擊生成的通知後。我怎樣才能做到這一點??一些立即回覆將不勝感激。它工作得很好(在用日曆添加switch語句之前)現在我正在收到通知。但點擊通知單擊通知後未打開新活動Android

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     Log.v("Message", "Alarm"); 

     String lang = ""; 
     int imageCode = 1; 

     NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

     Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); 
     long when = System.currentTimeMillis(); 
     Notification notification; 

     AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 

     Intent notificationIntent = new Intent(context, View.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

     SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context); 
     boolean enableNotificaiton = getPrefs.getBoolean("notifications", true);   
     String timePeriod = getPrefs.getString("period", "2"); 
     Calendar calA = Calendar.getInstance(); 

     switch(Integer.parseInt(timePeriod)){ 

     case 1: 
       //<item>Every 30 Minutes</item>     
       calA.add(Calendar.MINUTE, 1); 
       alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);    

      break; 

     case 2: 
      //<item>Hourly</item> 
      calA.add(Calendar.MINUTE, 60); 
      alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent); 


      break; 

     case 3: 
      //<item>Every 2 Hours</item> 
      calA.add(Calendar.MINUTE, 120); 
      alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent); 


      break; 

     case 4: 
      //<item>Every 6 Hours</item> 
      calA.add(Calendar.MINUTE, 60*6); 
      alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent); 


      break; 

     case 5: 
      //<item>Daily</item> 
      calA.add(Calendar.MINUTE, 60*24); 
      alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent); 

      break; 

     } 

     String language = getPrefs.getString("language","1"); 

     switch (Integer.parseInt(language)) { 

     case 1: 
      lang = "en"; 
      break; 

     case 2: 
      lang = "sin"; 
      break; 

     } 

     RemoteViews contentView = null; 

     if (lang.equals("en")) { 
      notification = new Notification(R.drawable.ic_launcher, "ProWeather - English", when); 
      contentView = new RemoteViews(context.getPackageName(), R.layout.view); 
      //contentView.setTextViewText(R.id.title, "English Notification"); 
     } else { 

      notification = new Notification(R.drawable.ic_launcher, "ProWeather - Sinhala", when); 
      contentView = new RemoteViews(context.getPackageName(), R.layout.view);  

      switch (imageCode) { 
      case 1: 
       Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sinhala); 
       contentView.setImageViewBitmap(R.id.ivText, bitmap2); 
       contentView.setTextViewText(R.id.tvDescrition, "Clear Weather"); 
       break; 

      case 2: 
       Bitmap bitmap3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sinhala2); 
       contentView.setImageViewBitmap(R.id.ivText, bitmap3); 
       contentView.setTextViewText(R.id.tvDescrition, "Heavy Rain"); 
       break; 

      case 3: 
       Bitmap bitmap4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sinhala3); 
       contentView.setImageViewBitmap(R.id.ivText, bitmap4); 
       contentView.setTextViewText(R.id.tvDescrition, "Heavy Rain with Lightning"); 
       break; 

      } 
     } 



     notification.contentView = contentView; 

     notification.flags = Notification.FLAG_AUTO_CANCEL; 

     notification.contentIntent = contentIntent; 

     // Cancel the notification after its selected 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     notification.defaults |= Notification.DEFAULT_LIGHTS; // LED 
     notification.defaults |= Notification.DEFAULT_VIBRATE; // Vibration 
     notification.defaults |= Notification.DEFAULT_SOUND; // Sound 

     // setting id a constant will replace previous notification with the 
     // new 
     notificationManager.notify(100, notification); 


    } 

} 

回答

1

使用下面的代碼後不開放新的活動:

 int icon = R.drawable.app_notification_icon; 
     long when = System.currentTimeMillis(); 

     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 

     String title = context.getString(R.string.app_name); 

     Intent notificationIntent = new Intent(context, 
       NotificationDialog.class); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     notificationIntent.putExtra(IntentConstantsUtils.MESSAGE, message); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, 
       notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Play default notification sound 
     notification.defaults |= Notification.DEFAULT_SOUND; 

     // Vibrate if vibrate is enabled 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(0, notification); 

創建一個新的Java類:

public class NotificationDialog extends Activity{ 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      String message = getIntent().getStringExtra(IntentConstantsUtils.MESSAGE); 
      showDialog(this, message, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        finish(); 
       } 
      }); 
     } 

private void showDialog(Context context, String message, 
      DialogInterface.OnClickListener onOkClick) { 
     AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

     // Setting Dialog Title 
     alertDialog.setTitle("Alert"); 

     // Setting Dialog Message 
     alertDialog.setMessage(message); 

     // Setting Icon to Dialog 
     // alertDialog.setIcon(R.drawable.tick); 

     // Setting OK Button 
     alertDialog.setButton("OK", onOkClick); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 
    } 

在清單中添加

<activity 
      android:name="com.shufflecloud.smg.activities.NotificationDialog" 
      android:screenOrientation="portrait" 
      android:theme="@android:style/Theme.Dialog" > 
     </activity> 
+0

Ypu is usin g PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT); 使用PendingIntent intent = PendingIntent.getActivity(context,0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);它應該工作。 – Swetank

+0

我嘗試的PendingIntent意圖= PendingIntent.getActivity(上下文,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);但仍然無法工作? :( –

+0

您正在使用,意圖notificationIntent =新意圖(背景下,View.class);調用自己的活動 – Swetank

0

更改

Intent notificationIntent = new Intent(context, View.class); 

Intent notificationIntent = new Intent(context, View.class, Intent.FLAG_ACTIVITY_NEW_TASK); 
0

這是奇巧4.4 問題嘗試用下面一行到你的活動要在通知

android:exported="true" 

點擊打開並添加標誌,以通知inttent

Notification.FLAG_AUTO_CANCEL;