Skip to content

Commit

Permalink
fix vibrate exception
Browse files Browse the repository at this point in the history
  • Loading branch information
zfdang committed May 27, 2016
1 parent 27e2e73 commit 5574a6a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE" android:maxSdkVersion="18"/>
<!-- <uses-permission android:name="android.permission.CAMERA" /> -->


<!-- permissions for shareSDK -->
<uses-permission android:name="android.permission.GET_TASKS"/>
<!-- <uses-permission android:name="android.permission.INTERNET" /> -->
Expand Down
44 changes: 24 additions & 20 deletions app/src/main/java/com/zfdang/zsmth_android/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,26 +281,30 @@ public void onReceiveResult(int resultCode, Bundle resultData) {


private void showNotification(String text) {

Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
notificationIntent.putExtra(SMTHApplication.SERVICE_NOTIFICATION_MESSAGE, text);
// http://stackoverflow.com/questions/26608627/how-to-open-fragment-page-when-pressed-a-notification-in-android
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("zSMTH提醒")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentText(text)
.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notificationID, notification);
// http://stackoverflow.com/questions/13602190/java-lang-securityexception-requires-vibrate-permission-on-jelly-bean-4-2
try{
Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
notificationIntent.putExtra(SMTHApplication.SERVICE_NOTIFICATION_MESSAGE, text);
// http://stackoverflow.com/questions/26608627/how-to-open-fragment-page-when-pressed-a-notification-in-android
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("zSMTH提醒")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentText(text)
.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notificationID, notification);
} catch (Exception se) {
Log.e(TAG, "showNotification: " + se.toString());
}
}

@Override
Expand Down

0 comments on commit 5574a6a

Please sign in to comment.