Shift of application when screen turns



  • When the application is activated every minute, the notification shall be scanned, but if it turns the screen and then returns to the reference position, it starts to scramble three times in a row. I understand each of the MainActivity, ListActivity and CheckBoxActivity. How can you avoid that?

    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);
    }
    }

    NotificationService.java

    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;
    

    </activity>

    <activity android:name=".activities.ListActivity"/>
    <activity android:name=".activities.CheckBoxActivity"/>

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

    </application>



  • There is a code:

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

    Which places an unmarked reference to MainActivity in NotificationService, which, according to the code, will be stored 710 * 60 milliseconds. It means that the garbage collector can't gather your activism when the screen turns over this time. And you need to collect it because when you're turning the screen, there's a new installation, that's two times you get 3 copies of the activate - from here and 3 notes.

    WeakHandler is a library from Badoo. http://habrahabr.ru/company/badoo/blog/240479/ This article says everything




Suggested Topics

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