Notice of notification - no mistakes, but not notice.



  • There's a service that should send a notification every minute. But this code doesn't work.

    public class MainActivity extends TabActivity {
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost tabHost = getTabHost();
    
    TabHost.TabSpec tabSpec;
    
    tabSpec = tabHost.newTabSpec("tag1");
    tabSpec.setIndicator(" List ");
    tabSpec.setContent(new Intent(this, ListActivity.class));
    tabHost.addTab(tabSpec);
    
    tabSpec = tabHost.newTabSpec("tag2");
    tabSpec.setIndicator(" CheckBox ");
    tabSpec.setContent(new Intent(this, CheckBoxActivity.class));
    tabHost.addTab(tabSpec);
    
    NotificationService.start(this);
    

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }
    
    return super.onOptionsItemSelected(item);
    

    }

    public class NotificationService extends IntentService {

    public NotificationService() {
    super("NotificationService");
    }

    private static final String START_SEND_NOTIFICATION = "start_send_notification";
    private final Handler handler = new Handler();
    private NotificationManager notificationManager;

    // method for start service
    public static void start(Context context) {
    Intent intent = new Intent(context, NotificationService.class);
    intent.setAction(START_SEND_NOTIFICATION);
    context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    if (intent != null) {
    final String action = intent.getAction();
    switch (action) {
    case START_SEND_NOTIFICATION:
    showNotification();
    runnable.run();
    break;
    }
    }
    }

    private int counter = 0;

    private Runnable runnable = new Runnable() {
    public void run() {
    System.out.println("Conunt: " + counter);
    counter++;
    if (counter>60){
    showNotification();
    counter = 0;
    }

        handler.postDelayed(this, 710);
    
        //handler.removeCallbacks(this);
    
    }
    

    };

    private void showNotification() {
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    System.out.println("<<Showing notification>>");

    Notification.Builder builder = new Notification.Builder(getApplicationContext());
    builder.setTicker("New notification")
            .setWhen(System.currentTimeMillis())
    

    // .setAutoCancel(true)
    .setContentText("Hello")
    .setProgress(100,20,true);

    Notification notification = builder.build();
    
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    notification.defaults |= Notification.DEFAULT_SOUND;
    
    
    notificationManager.notify(1,notification);
    

    }
    }

    AndroidManifest.xml

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
    android:name=".activities.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />

            &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
    
    &lt;activity android:name=".activities.ListActivity"/&gt;
    &lt;activity android:name=".activities.CheckBoxActivity"/&gt;
    
    &lt;service android:name=".service.NotificationService"/&gt;
    

    </application>



  • According to the https://stackoverflow.com/a/16448278/3212712 The problem may be that there is no need for notification. Try to add it:

    builder.setTicker("New notification")
    .setWhen(System.currentTimeMillis())
    .setContentText("Hello")
    .setProgress(100,20,true)
    .setSmallIcon(R.drawable.ic_launcher);
    

    I also advise you not to. TabActivity, which is an outdated leather API 13

    And class. Notification too. Use better. NotificationCompat




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2