如何将我的代码插入到startForeground方法Android中

时间:2016-12-07 15:27:57

标签: service

我想将我的代码运行到后台,例如这个循环,无论如何:

for (int i = 1; i<=500; i++) {
                    System.out.println(i);
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

我必须在哪里插入此代码?

MainActivity:

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

MyService包含这个:

Intent notificationIntent = new Intent(this, MainActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(0)
                .setContentTitle("My Awesome App")
                .setContentText("Doing some work...")
                .setContentIntent(pendingIntent).build();

        startForeground(1337, notification);

0 个答案:

没有答案