1

我想在我的應用程序中發出通知。通知服務和接收器

如果設備重新啓動 但接收方不能使用,接收方必須打開通知服務。

這裏是我的代碼>>

服務代碼

public class MyService extends Service { 

String myringtone; 
long[] myviber; 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    Intent intent1 =new Intent(this,MyService.class); 

    Bundle bundle = new Bundle(); 
    bundle=intent.getExtras(); 
    myviber=bundle.getLongArray("veberationdata"); 
    myringtone=bundle.getString("ringtonedata"); 

    intent1.putExtra("ringtonedata", myringtone); 
    intent1.putExtra("veberationdata", myviber); 

    AlarmManager manager=(AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
    PendingIntent pendingIntent1= PendingIntent.getService(this, 0, intent1, 0); 
    manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10000,0,pendingIntent1); 

    NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext()); 
    //  notification.setAutoCancel(true); 
    notification.setSmallIcon(R.drawable.war_pic_white); 
    long[] viber_no=new long[]{100, 200, 100, 500}; 
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
    String savedringtone = sharedPreferences.getString("ringtonefornoti", null); 
    Uri ringtoneUri = Uri.parse(savedringtone); 

    notification.setTicker(".......M E S S E G......."); 
    notification.setContentTitle("Title of noti"); 
    notification.setContentText("summary "); 
    notification.setSound(ringtoneUri); 
    notification.setVibrate(viber_no); 

    Intent i = new Intent(this,MyService.class); 

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.setContentIntent(pendingIntent); 

    long time = new Date().getTime(); 
    String tmpStr = String.valueOf(time); 
    String last4Str = tmpStr.substring(tmpStr.length() - 5); 
    int notificationId = Integer.valueOf(last4Str); 

    NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(notificationId,notification.build()); 
    return START_STICKY; 

} 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 
} 

我的接收機

public class MyReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 

    Bundle bundle = new Bundle(); 
    bundle = intent.getExtras(); 

    String mystate = bundle.getString("state"); 

    if (mystate.equals("ON")) { 

     Intent serviceintent = new Intent(context, MyService.class); 
     context.startService(serviceintent); 
    } 
    else if (mystate.equals("OFF")) 
    { 
     Intent serviceintent = new Intent(context, MyService.class); 
     context.stopService(serviceintent); 
    } 
} 

}

我制定活動

public class SettingPage extends PreferenceActivity { 

     boolean viber_sw_on_off,notifi_sw_on_off; 
     int savedhour , savedmint , savedviber; 
     long[] viber_no; 
     String savedringtone, typeofnotify; 

     SwitchPreference notifi_switch, viber_switch; 
     ListPreference list_pref; 
     Preference time_pref; 
     RingtonePreference ring_pref; 

     SharedPreferences sharedPreferences ,savedpref; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    addPreferencesFromResource(R.xml.mypref); 

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 

    notifi_switch = (SwitchPreference) findPreference("notifications_switch"); 
    viber_switch = (SwitchPreference) findPreference("vibrate_switch"); 
    list_pref  = (ListPreference) findPreference("list_ofnotify"); 
    time_pref  = (Preference) findPreference("time_pre"); 
    ring_pref  = (RingtonePreference) findPreference("ringtonefornoti"); 

    savedpref= getSharedPreferences("MySetting", MODE_PRIVATE); 
    savedhour = savedpref.getInt("phour", 0); 
    savedmint = savedpref.getInt("pmint", 0); 
    savedringtone = savedpref.getString("mring",null); 
    savedviber = savedpref.getInt("mveb",1); 


    if (savedhour != 0) 
    { 
     time_pref.setSummary(savedhour + ":" + savedmint); 
    } 

    if (savedringtone == null) 
    { 
     savedringtone =(Settings.System.DEFAULT_NOTIFICATION_URI.getPath()); 
    } 

    if (savedviber == 0) 
    { 
     viber_no=new long[]{0}; 

    } 
    else 
    { 
     viber_no=new long[]{100, 200, 100, 500}; 
    } 






    notifi_switch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
     @Override 
     public boolean onPreferenceChange(Preference preference, Object o) { 

      setNotification(); 
      return true; 
     } 
    }); 

    list_pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
     @Override 
     public boolean onPreferenceChange(Preference preference, Object o) { 

      setnotify(); 
      return true; 
     } 
    }); 

    time_pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { 
     @Override 
     public boolean onPreferenceClick(Preference preference) { 

      setTime_pref(); 
      return true; 
     } 
    }); 


    viber_switch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
     @Override 
     public boolean onPreferenceChange(Preference preference, Object o) { 

      setViberation(); 
      return true; 
     } 
    }); 

    ring_pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
     @Override 
     public boolean onPreferenceChange(Preference preference, Object o) { 

      setring(); 
      return true; 
     } 
    }); 
} 




public void setTime_pref() { 

    Calendar c = Calendar.getInstance(); 
    final int mHour = c.get(Calendar.HOUR_OF_DAY); 
    final int mMinute = c.get(Calendar.MINUTE); 

    TimePickerDialog tpd = new TimePickerDialog(this, 
      new TimePickerDialog.OnTimeSetListener() { 

       @Override 
       public void onTimeSet(TimePicker view, int hourOfDay, 
             int minute) { 
        time_pref.setSummary(hourOfDay + ":" + minute); 

        SharedPreferences Mytime = getSharedPreferences("MySetting",MODE_PRIVATE); 
        SharedPreferences.Editor editor = Mytime.edit(); 
        editor.putInt("phour",mHour); 
        editor.putInt("pmint",mMinute); 
        editor.commit(); 
       } 
      }, mHour, mMinute, false); 
    tpd.show(); 
} 


public void setNotification() 
{ 
    notifi_sw_on_off = sharedPreferences.getBoolean("notifications_switch", true); 

    MyReceiver myReceiver = new MyReceiver(); 

    if (notifi_sw_on_off == false) 
    { 


     Intent broadcastintent = new Intent(getBaseContext(),MyReceiver.class); 

     broadcastintent.putExtra("state", "ON"); 

     sendBroadcast(broadcastintent); 


     registerReceiver(myReceiver,new IntentFilter("com.example.pushups.MyReceiver")); 

     Bundle bundle = new Bundle(); 

     Intent serviceintent = new Intent(getBaseContext(), MyService.class); 

     bundle.putString("ringtonedata",savedringtone); 
     bundle.putLongArray("veberationdata", viber_no); 
     serviceintent.putExtras(bundle); 

     startService(serviceintent); 

    } 
    else if (notifi_sw_on_off == true) 
    { 

     Intent broadcastintent = new Intent(getBaseContext(),MyReceiver.class); 

     broadcastintent.putExtra("state","OFF"); 

     sendBroadcast(broadcastintent); 

     Intent serviceintent = new Intent(getBaseContext(), MyService.class); 
     stopService(serviceintent); 

     unregisterReceiver(myReceiver); 


    } 
} 


public void setring() 
{ 

    String savedringtone = sharedPreferences.getString("ringtonefornoti", null); 


    SharedPreferences myringtone = getSharedPreferences("MySetting",MODE_PRIVATE); 
    SharedPreferences.Editor editor = myringtone.edit(); 
    editor.putString("mring", savedringtone); 
    editor.commit(); 

} 

public void setViberation() 
{ 
    viber_sw_on_off = sharedPreferences.getBoolean("vibrate_switch", false); 

    if (viber_sw_on_off == false) 
    { 
     viber_no=new long[]{100, 200, 100, 500}; 


     SharedPreferences myviberation = getSharedPreferences("MySetting",MODE_PRIVATE); 
     SharedPreferences.Editor editor = myviberation.edit(); 
     editor.putInt("mveb", 1); 
     editor.commit(); 
    } 
    else if(viber_sw_on_off == true) 
    { 
     viber_no=new long[]{0}; 

     SharedPreferences myviberation = getSharedPreferences("MySetting",MODE_PRIVATE); 
     SharedPreferences.Editor editor = myviberation.edit(); 
     editor.putInt("mveb", 0); 
     editor.commit(); 
    } 

} 


public void setnotify() 
{ 
    typeofnotify = sharedPreferences.getString("list_pref","Defult_Value"); 

    String s = list_pref.getEntry().toString(); 

    Toast.makeText(getApplicationContext(), "" + s, Toast.LENGTH_LONG).show(); 

    if (typeofnotify.equals("")) 
    { 

    } 

} 

} 

我的清單

 <service 
     android:name=".MyService"> 
    </service> 

    <receiver android:name=".MyReceiver" android:enabled="true" android:exported="false"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
      <action android:name="com.example.pushups.MyReceiver"/> 
     </intent-filter> 
    </receiver> 


</application>  <uses-permission android:name="android.permission.VIBRATE"/> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

hehe an image

回答

0

檢查下列項目

  1. 坐落在MyReceiver.class一個log.v的onReceive對收到的作品或沒有。
  2. 如果它的工作原理,設置一個log.v知道「mystate」 的值,如果它的工作原理,檢查Startservice的意圖(有時sharedPreferences值不正確保存)
  3. 然後。 (有時意向didnt由於語境問題工作)

4.如果服務啓動正常,則檢查通知管理器正確。

5.卸載不需要的第三方應用程序。

注:

最佳解決方案開始&一站式服務,以避免上下文問題:

  1. startService(new Intent(YourActivity.this, MyService.class));
  2. stopService(new Intent(YourActivity.this, MyService.class));
+0

仍然沒有工作! –