List of usage examples for android.app PendingIntent getActivity
public static PendingIntent getActivity(Context context, int requestCode, Intent intent, @Flags int flags)
From source file:be.ehb.fallwear.MyGcmListenerService.java
/** * Create and show a simple notification containing the received GCM message. * * @param message GCM message received.//from w ww . ja va 2 s. c o m */ private void sendNotification(String message) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_ic_notification).setContentTitle("GCM Message") .setContentText(message).setAutoCancel(true).setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); }
From source file:com.projecttango.experiments.basictango_button.MainActivity.java
public void togglestate(View view) { isOn = ((ToggleButton) view).isChecked(); // Gets an instance of the NotificationManager service NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Intent for Stop button on Notification Intent intent = new Intent(this, MainActivity.class); intent.putExtra("isOn", "false"); PendingIntent aIntent = PendingIntent.getActivity(this, 0, intent, 0); //Intent for Restart button on Notification Intent intent2 = new Intent(this, RestartActivity.class); intent.putExtra("isOn", "true"); PendingIntent bIntent = PendingIntent.getActivity(this, 0, intent2, 0); //NOTIFICATION SETUP NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.appicon) .setContentTitle("UTC Tango").setContentText("Collecting Data!").setOngoing(true).setPriority(2) .setAutoCancel(true).setDeleteIntent(aIntent).addAction(R.drawable.none, "Stop Session", aIntent) .addAction(R.drawable.none, "Restart", bIntent); int mNotificationId = 8001; //Notification ID if (isOn) {//from ww w. j a va 2 s . c om //create UUID for session; uuid = UUID.randomUUID(); //Starting Toast Toast.makeText(getApplicationContext(), "Collecting Data...", Toast.LENGTH_SHORT).show(); // Builds the notification and issues it. mNotifyMgr.notify(mNotificationId, mBuilder.build()); } else { //action if button is false isOn = false; mNotifyMgr.cancelAll(); //kill notification //Stopping Toast Toast.makeText(getApplicationContext(), "Stopped collecting data", Toast.LENGTH_SHORT).show(); } }
From source file:fr.bmartel.android.tictactoe.gcm.MyGcmListenerService.java
/** * Called when message is received.// ww w .j av a 2 s . co m * * @param from SenderID of the sender. * @param data Data bundle containing message data as key/value pairs. * For Set of keys use data.keySet(). */ // [START receive_message] @Override public void onMessageReceived(String from, Bundle data) { String message = data.getString("message"); if (from.startsWith("/topics/" + GameSingleton.DEVICE_ID)) { Log.d(TAG, "Message: " + message); try { JSONObject object = new JSONObject(message); ArrayList<String> eventItem = new ArrayList<>(); eventItem.add(object.toString()); broadcastUpdateStringList(BroadcastFilters.EVENT_MESSAGE, eventItem); if (!GameSingleton.activityForeground) { if (object.has(RequestConstants.DEVICE_MESSAGE_TOPIC) && object.has(RequestConstants.DEVICE_MESSAGE_CHALLENGER_ID) && object.has(RequestConstants.DEVICE_MESSAGE_CHALLENGER_NAME)) { GameMessageTopic topic = GameMessageTopic .getTopic(object.getInt(RequestConstants.DEVICE_MESSAGE_TOPIC)); ChallengeMessage challengeMessage = new ChallengeMessage(topic, object.getString(RequestConstants.DEVICE_MESSAGE_CHALLENGER_ID), object.getString(RequestConstants.DEVICE_MESSAGE_CHALLENGER_NAME)); Log.i(TAG, "challenged by " + challengeMessage.getChallengerName() + " : " + challengeMessage.getChallengerId()); Intent intent2 = new Intent(this, DeviceListActivity.class); intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent2, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Fight!") .setContentText("challenged by " + challengeMessage.getChallengerName()) .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(new Random().nextInt(9999), notificationBuilder.build()); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); boolean isScreenOn = pm.isScreenOn(); if (isScreenOn == false) { PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyLock"); wl.acquire(10000); PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCpuLock"); wl_cpu.acquire(10000); } GameSingleton.pendingChallengeMessage = challengeMessage; GameSingleton.pendingChallenge = true; } } } catch (JSONException e) { e.printStackTrace(); } } }
From source file:asia.covisoft.goom.gcm.GoOmUserGcmListenerService.java
/** * Create and show a simple notification containing the received GCM message. * * @param message GCM message received./*from ww w . j av a 2s .com*/ */ private void sendNotification(String message, Driverconfirm response) { Intent intent = new Intent(this, HistoryDetailsActivity.class); trandingId = response.getTradingid(); intent.putExtra(Extras.TRADING_ID, trandingId); intent.putExtra(Extras.HISTORY_STATE, false); if (response.getValue().equals("tip")) { intent.putExtra(Extras.REQUEST_TIP, true); intent.putExtra(Extras.MAX_TIP, response.getMaxsuggest()); intent.putExtra(Extras.MIN_TIP, response.getMinsuggest()); cancelTip(); } intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_ic_notification).setContentTitle(getString(R.string.app_name_full)) .setContentText(message).setAutoCancel(true).setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); }
From source file:com.nfc.gemkey.MainActivity.java
@Override public void onCreate(Bundle icicle) { if (DEBUG)/*from ww w . j ava 2 s .c o m*/ Log.d(TAG, "onCreate"); super.onCreate(icicle); setContentView(R.layout.activity_main); mAdapter = NfcAdapter.getDefaultAdapter(this); // Create a generic PendingIntent that will be deliver to this activity. The NFC stack // will fill in the intent with the details of the discovered tag before delivering to // this activity. mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); // Setup an intent filter for all MIME based dispatches IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); tagDetected.addCategory(Intent.CATEGORY_DEFAULT); mFilters = new IntentFilter[] { tagDetected }; mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); registerReceiver(mUsbReceiver, filter); if (getLastNonConfigurationInstance() != null) { Log.i(TAG, "open usb accessory@onCreate"); mAccessory = (UsbAccessory) getLastNonConfigurationInstance(); openAccessory(mAccessory); } buttonLED = (ToggleButton) findViewById(R.id.nfc_btn); buttonLED.setBackgroundResource( buttonLED.isChecked() ? R.drawable.btn_toggle_yes : R.drawable.btn_toggle_no); buttonLED.setOnCheckedChangeListener(mKeyLockListener); tagId = (TextView) findViewById(R.id.nfc_tag); tagId.setText(R.string.nfc_scan_tag); // Avoid NetworkOnMainThreadException StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites() .detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects() .detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); setVolumeControlStream(AudioManager.STREAM_MUSIC); }
From source file:com.fastbootmobile.rssdemo.PushReceiver.java
@Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "ACTION : " + intent.getAction()); // Debug log the intent "action" // Get the shared preferences for OwnPush keys SharedPreferences pref = context.getApplicationContext().getSharedPreferences(OwnPushClient.PREF_PUSH, Context.MODE_PRIVATE); if (intent.getAction().equals(OwnPushClient.INTENT_RECEIVE)) { // This is a push message Log.d(TAG, "Decrypt : " + intent.getExtras().getString(OwnPushClient.EXTRA_DATA)); OwnPushCrypto fp = new OwnPushCrypto(); // Create a crypto object for decrypt // Get the app key pair from shared preferences (these have been confirmed by the register intent) OwnPushCrypto.AppKeyPair keys = fp.getKey(pref.getString(OwnPushClient.PREF_PUBLIC_KEY, ""), pref.getString(OwnPushClient.PREF_PRIVATE_KEY, "")); // Decrypt the message from the intent extra data String msg = fp.decryptFromIntent(intent.getExtras(), BuildConfig.APP_PUBLIC_KEY, keys); JSONObject jObj;//from w w w. ja v a 2 s .co m if (msg != null) { Log.e(TAG, "RSS : " + msg); try { // Decode the JOSN data in the message jObj = new JSONObject(msg); Intent i = new Intent(Intent.ACTION_VIEW); // Create the intent i.setData(Uri.parse(jObj.getString("link"))); // Set the data using URI (these are web links) // Convert to pending intent PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), i, 0); // Create the notification Notification n = new Notification.Builder(context.getApplicationContext()) .setContentTitle("OwnPush RSS") // Main Title .setContentText(jObj.getString("title")) // Set content .setContentIntent(pIntent) // Add the pending intent .setSmallIcon(R.drawable.ic_done).setAutoCancel(true) // Remove notification if opened .build(); n.defaults |= Notification.DEFAULT_SOUND; // Make some noise on push // Get the notification manager and display notification NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notification_num, n); // Increase the notification counter by 1 notification_num++; } catch (Exception e) { return; } } } }
From source file:com.rossier.shclechelles.service.MyGcmListenerService.java
/** * Create and show a simple notification containing the received GCM message. * * @param message GCM message received.// w ww . java 2s . c om */ private void sendNotification(String from, String message) { Gson gson = new GsonBuilder().create(); String messageUTF8 = ""; try { messageUTF8 = new String(message.getBytes(), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Log.i("MyGCMListenerService", message); Log.i("MyGCMListenerService", messageUTF8); if (messageUTF8 == "") return; MatchNotification match = gson.fromJson(messageUTF8, MatchNotification.class); Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); int icon = R.drawable.ic_launcher_shc; long when = System.currentTimeMillis(); // Notification notification = new Notification(icon, "Nouveaux rsultats", when); Notification notification = new Notification.Builder(this.getBaseContext()) .setContentText("Nouveaux rsultats").setSmallIcon(icon).setWhen(when).build(); NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.match_notif_layout); contentView.setTextViewText(R.id.notif_team_home, match.getTeam_home()); contentView.setTextViewText(R.id.notif_team_away, match.getTeam_away()); contentView.setTextViewText(R.id.notif_result_home, match.getResult_home() + ""); contentView.setTextViewText(R.id.notif_result_away, match.getResult_away() + ""); contentView.setTextViewText(R.id.notif_ligue, match.getLigue()); contentView.setImageViewResource(R.id.notif_team_loc_logo, Utils.getLogo(match.getTeam_home())); contentView.setImageViewResource(R.id.notif_team_away_logo, Utils.getLogo(match.getTeam_away())); notification.contentView = contentView; Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.contentIntent = contentIntent; //notification.flags |= Notification.FLAG_ONGOING_EVENT; //Do not clear the notification notification.defaults |= Notification.DEFAULT_LIGHTS; // LED notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration notification.defaults |= Notification.DEFAULT_SOUND; // Sound //get topics name String[] topic = from.split("/"); mNotificationManager.notify(topic[2].hashCode(), notification); }
From source file:com.piusvelte.sonet.core.PhotoUploadService.java
private void start(Intent intent) { if (intent != null) { String action = intent.getAction(); if (Sonet.ACTION_UPLOAD.equals(action)) { if (intent.hasExtra(Accounts.TOKEN) && intent.hasExtra(Statuses.MESSAGE) && intent.hasExtra(Widgets.INSTANT_UPLOAD)) { String place = null; if (intent.hasExtra(Splace)) place = intent.getStringExtra(Splace); String tags = null; if (intent.hasExtra(Stags)) tags = intent.getStringExtra(Stags); // upload a photo Notification notification = new Notification(R.drawable.notification, "uploading photo", System.currentTimeMillis()); notification.setLatestEventInfo(getBaseContext(), "photo upload", "uploading", PendingIntent.getActivity(PhotoUploadService.this, 0, (Sonet.getPackageIntent(PhotoUploadService.this, About.class)), 0)); ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFY_ID, notification);/* ww w . j a v a 2s.co m*/ (new AsyncTask<String, Void, String>() { @Override protected String doInBackground(String... params) { String response = null; if (params.length > 2) { Log.d(TAG, "upload file: " + params[2]); HttpPost httpPost = new HttpPost(String.format(FACEBOOK_PHOTOS, FACEBOOK_BASE_URL, Saccess_token, mSonetCrypto.Decrypt(params[0]))); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); File file = new File(params[2]); ContentBody fileBody = new FileBody(file); entity.addPart(Ssource, fileBody); HttpClient httpClient = SonetHttpClient .getThreadSafeClient(getApplicationContext()); try { entity.addPart(Smessage, new StringBody(params[1])); if (params[3] != null) entity.addPart(Splace, new StringBody(params[3])); if (params[4] != null) entity.addPart(Stags, new StringBody(params[4])); httpPost.setEntity(entity); response = SonetHttpClient.httpResponse(httpClient, httpPost); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } } return response; } @Override protected void onPostExecute(String response) { // notify photo success String message = getString(response != null ? R.string.success : R.string.failure); Notification notification = new Notification(R.drawable.notification, "photo upload " + message, System.currentTimeMillis()); notification.setLatestEventInfo(getBaseContext(), "photo upload", message, PendingIntent.getActivity(PhotoUploadService.this, 0, (Sonet.getPackageIntent(PhotoUploadService.this, About.class)), 0)); ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFY_ID, notification); stopSelfResult(mStartId); } }).execute(intent.getStringExtra(Accounts.TOKEN), intent.getStringExtra(Statuses.MESSAGE), intent.getStringExtra(Widgets.INSTANT_UPLOAD), place, tags); } } } }
From source file:org.bfr.periodicquery.PeriodicQueryService.java
@Override public int onStartCommand(Intent intent, int flags, int startId) { // The intent to launch when the user clicks the expanded notification Intent launchIntent = new Intent(this, StartStopActivity.class); launchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendIntent = PendingIntent.getActivity(this, 0, launchIntent, 0); // This constructor is deprecated. Use Notification.Builder instead Notification notice = new Notification(R.drawable.ic_launcher, "Periodic Query", System.currentTimeMillis()); // This method is deprecated. Use Notification.Builder instead. notice.setLatestEventInfo(this, "Periodic Query", "Periodic Query", pendIntent); notice.flags |= Notification.FLAG_NO_CLEAR; startForeground(1234, notice);//from w ww .j a v a 2 s.com return START_STICKY; }