I need to make an application that creates a service and runs in the background sending push notifications after a certain time, I also need to be able to change the message text using EditText. Just with the change of the text I have a problem. I was prompted to use broadcastReceiver. And that's what I got: Main activity:

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button enter = (Button) findViewById(R.id.enter); assert enter != null; enter.setOnClickListener(new View.OnClickListener() { //подтверждение ввода @Override public void onClick(View v) { Intent intent = new Intent("Intent"); EditText editText = (EditText) findViewById(R.id.edit); intent.putExtra("message",editText.getText().toString()); intent.setAction("broadcast"); sendBroadcast(intent); } }); Button btnStart = (Button)findViewById(R.id.btn_start); Button btnStop = (Button) findViewById(R.id.btn_stop); // запуск службы assert btnStart != null; btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startService(new Intent(MainActivity.this, MyService.class)); } }); //Остановка assert btnStop != null; btnStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopService(new Intent(MainActivity.this, MyService.class)); } }); } } 

BroadCast:

 public class BroadCast extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { CharSequence data = intent.getCharSequenceExtra("message"); MyService service = null; try { service = new MyService(); } catch (InterruptedException e) { e.printStackTrace(); } if(service!=null) { 

service.receiveNumbers = (Editable) intent.getCharSequenceExtra (String.valueOf (data)); }}}

Service:

 public class MyService extends Service{ Editable receiveNumbers; NotificationManager nm; Notification notification; public MyService() throws InterruptedException { } @Override public int onStartCommand(Intent intent, int flags, int startId) { return Service.START_STICKY; } @Override public void onCreate() { super.onCreate(); Log.d("MYLOG","onCreate"); nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(getApplicationContext()); Intent intent = new Intent(getApplicationContext(),MainActivity.class); PendingIntent pendingIntetnt = PendingIntent.getActivity(getApplicationContext(),0,intent,PendingIntent.FLAG_CANCEL_CURRENT); MainActivity ma = new MainActivity(); builder.setContentIntent(pendingIntetnt) .setSmallIcon(R.drawable.ic_adb_black_48dp) .setLargeIcon(BitmapFactory.decodeResource(getApplication().getResources(),R.drawable.ic_adb_black_48dp)) .setTicker("Сообщалка") .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setContentTitle("Look") .setContentText(receiveNumbers); notification = builder.build(); Timer timer = new Timer(); TimerTask timerTask = new TimerTask() { @Override public void run() { nm.notify(1, notification); } }; timer.schedule(timerTask,0,60000); } private void broadcastReceiver(Editable text) { } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.d("MYLOG","onStart"); } @Override public void onDestroy() { super.onDestroy(); Log.d("MYLOG","onDestroy"); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } } 

Manifest:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.vkramarenko.service"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver //Здесь есть еще один ресивер который должен перезапускать сервис после перезагрузки android:name=".BootCompleteReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.REBOOT"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> <receiver android:name=".BroadCast"> <intent-filter> <action android:name="broadcast"/> </intent-filter> </receiver> <service android:name=".MyService" android:enabled="true" android:exported="true"></service> </application> </manifest> 

    1 answer 1

    fast fix:

    instead

     intent.putExtra("message",editText.getText()); 

    this

     intent.putExtra("message",editText.getText().toString()); 
    • Now NullPointerException - Vlad Kramarenko
    • reset the logs exactly where the NPE? - s_klepcha
    • I had a small overturn there, but I corrected it and still the error is deeper, now the notification is just displayed without the text. - Vlad Kramarenko