0

我是新開發android應用程序和學習開發基本應用程序和功能。 我要創建通知設置就像WhtsApp也即創建推送通知設置,如WhatsApp的「通知」設置android

  1. 通知音(所有默認音表)
  2. 震動:默認情況下(或者我自己的自定義:關閉,默認情況下,短,長)

我希望像這個應用程序的基本功能,當設備收到新的通知。 我正在使用GCM發送和接收通知。 這將是很大的幫助。提前致謝。我想

通知菜單是這樣的: http://i.stack.imgur.com/vanda.jpg

編輯:

SettingsActivity.java

import android.app.Activity; 
import android.app.Notification; 
import android.app.Service; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.Bundle; 
import android.os.Vibrator; 
import android.view.Menu; 
import android.widget.CompoundButton; 
import android.widget.Toast; 
import android.widget.ToggleButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 

public class SettingsActivity extends Activity { 

Vibrator vibrate; 
ToggleButton tb; 
boolean status= false; 
public static final String MyPREFERENCES = "MyPrefs"; 
SharedPreferences prefs; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_settings); 
    tb = (ToggleButton) findViewById(R.id.toggleButton1); 
    prefs = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
    if (prefs.contains("vibrate")) { 
     status=Boolean.parseBoolean(prefs.getString("vibrate", "")); 
     if(status){ 
      tb.setChecked(true); 
     }else{ 
      tb.setChecked(false); 
     } 
    } 

    tb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
       boolean isChecked) { 
      // TODO Auto-generated method stub 
      if (isChecked) { 
       Toast.makeText(getApplicationContext(), "ON", 
         Toast.LENGTH_LONG).show(); 
       status= true; 
       Editor editor = prefs.edit(); 
       editor.putString("vibrate", status+""); 
       editor.commit(); 
       PreferencesData.saveString(getApplicationContext(), "vibrate", status+""); 
      } else { 
       Toast.makeText(getApplicationContext(), "OFF", 
         Toast.LENGTH_LONG).show(); 
       status= false; 
       Editor editor = prefs.edit(); 
       editor.putString("vibrate", status+""); 
       editor.commit(); 
       PreferencesData.saveString(getApplicationContext(), "vibrate", status+""); 

      } 
     } 
    }); 
} 

public boolean isVibrate(){ 

    return status; 
} 
} 

GCMIntentService.java

//some code 

private static void generateNotification(Context context, String message) { 
    int icon = R.drawable.ic_launcher; 
    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, MainActivity.class); 
    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = 
      PendingIntent.getActivity(context, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

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

    SettingsActivity stng = new SettingsActivity(); 
    Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); 

    boolean vibStatus = stng.isVibrate(); 

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3"); 

    // Vibrate if vibrate is enabled 
    if(vibStatus){ 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    vibrator.vibrate(500); 
    } 
    else{ 
     vibrator.cancel(); 
    } 
    notificationManager.notify(0, notification);  

} 
+0

您需要爲此創建'偏好屏幕'。 –

+0

我如何創建像WhatsApp提供的設置來設置自定義通知聲音和振動。你能告訴我任何例子或源代碼嗎? –

+0

轉至此:[http://viralpatel.net/blogs/android-preferences-activity-example/](http://viralpatel.net/blogs/android-preferences-activity-example/)and/or [android -quick-preferences-tutorial.html](http://www.javacodegeeks.com/2011/01/android-quick-preferences-tutorial.html) –

回答

1

創建Constant Class

public class Constant { 
public static boolean IS_VIBRATE = false; 
} 

現在,實現你Toggle Button OnCheckedChangeListener喜歡

tb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, 
      boolean isChecked) { 
     // TODO Auto-generated method stub 
     if (isChecked) { 
      Toast.makeText(getApplicationContext(), "ON", 
        Toast.LENGTH_LONG).show(); 
      Constant.IS_VIBRATE = true; 
     } else { 
      Toast.makeText(getApplicationContext(), "OFF", 
        Toast.LENGTH_LONG).show(); 
      Constant.IS_VIBRATE = false; 
     } 
    } 

現在,請將您的Notification Vibrate條件像

private static void generateNotification(Context context, String message) { 
int icon = R.drawable.ic_launcher; 
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, MainActivity.class); 
// set intent so it does not start a new activity 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
     Intent.FLAG_ACTIVITY_SINGLE_TOP); 
PendingIntent intent = 
     PendingIntent.getActivity(context, 0, notificationIntent, 0); 
notification.setLatestEventInfo(context, title, message, intent); 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 

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

//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3"); 

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

嘗試按我的答案,讓我知道