我试图在我的申请中发出通知。
如果设备重新启动,接收方必须打开通知服务 但它没有用。
这是我的代码>>
服务代码
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"/>
答案 0 :(得分:0)
检查以下内容
4.如果服务正常启动,请正确检查通知管理器。
5.卸载不需要的第三方应用程序。
注意:
最佳解决方案启动&amp;停止服务以避免上下文问题:
startService(new Intent(YourActivity.this, MyService.class));
stopService(new Intent(YourActivity.this, MyService.class));