2014-11-06 82 views
-3

我有一个Android应用程序,我想从服务显示通知或吐司每10秒例如,当应用程序被关闭或完成如何可以显示从服务敬酒每10秒

+3

“我有这样的课吗?请编辑它。”不,我们不是免费的编辑服务。解决这个问题,并提出一个具体的问题,你卡住了,我们会帮助你尽我们所能:) – ElGavilan 2014-11-06 13:49:44

+0

我的英语很差! – user3404171 2014-11-06 13:51:39

+0

user3404171不是。我从来没有想过,你不会说它作为第一语言 – ElGavilan 2014-11-06 13:53:04

回答

1

我有一个样品活性,服务等级和Timer类提供如下。在应用程序中使用类似的实现。

活动课

import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.Button; 

public class Sample extends Activity { 

Button button1,button2; 
private Handler mHandler = new Handler(); 

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

    // Call the start and stop method when needed.  

} 


public void Start(View v) 
{ 
    startService(new Intent(MainActivity.this , Sample_service.class)); 

} 

public void Stop(View v) 
{ 
    stopService(new Intent(MainActivity.this , Sample_service.class)); 

} 

} 

服务类

package com.example.connect; 

import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.widget.Toast; 

public class Sample_service extends Service{ 

Timer timer = new Timer(); 
TimerTask updateProfile = new CustomTimerTask(Sample_service.this); 

public void onCreate() { 

    super.onCreate(); 

    Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show(); 
    timer.scheduleAtFixedRate(updateProfile, 0, 10000); 

} 


@Override 
public void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show(); 
    timer.cancel(); 
} 



@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

} 

Timer类

package com.example.connect; 

import java.util.TimerTask; 

import android.content.Context; 
import android.os.Handler; 
import android.widget.Toast; 
public class CustomTimerTask extends TimerTask { 


private Context context; 
private Handler mHandler = new Handler(); 

public CustomTimerTask(Context con) { 
    this.context = con; 
} 



@Override 
public void run() { 
    new Thread(new Runnable() { 

     public void run() { 

      mHandler.post(new Runnable() { 
       public void run() { 
        Toast.makeText(context, "DISPLAY YOUR MESSAGE", Toast.LENGTH_SHORT).show(); 

       } 
      }); 
     } 
    }).start(); 

} 

} 
+0

什么是“Connect_service”上的“Sample_service”类? – user3404171 2014-11-06 14:32:16

0

从技术上讲,当任务执行你在onCreate()方法中写的,它会自动关闭。为此,您可以使用定时器/调度程序。

实施例:

Timer timer; 
TimerTask timerTask; 

timer.schedule(timerTask, 5000, 10000); 

timerTask = new TimerTask() { 
      public void run() { 

       //use a handler to run a toast that shows the current timestamp 
       handler.post(new Runnable() { 
        public void run() {      
         Toast toast = Toast.makeText(getApplicationContext(), strDate, duration); 
         toast.show(); 
        } 
       }); 
      } 
}; 
+0

我该如何使用此代码? – user3404171 2014-11-06 13:59:42

+0

您可以在任何地方使用此代码,在活动或服务类onCreate()中。 – samsad 2014-11-06 14:01:21

+0

在服务类onCreate()中使用这段代码。它会工作:) – samsad 2014-11-06 14:02:15

0

不要忘了在清单文件中添加应用程序标签中为您服务:

<service android:name=".ServiceGPS" 
     android:permission="[Add permission here if exists]" 
     android:label="[service name]" android:exported="true" 
     android:enabled="true"> 
</service> 
2

我在这里给出了三个文件,MainActivity,Manifest和MyService文件在你的应用程序中实现它,它将显示每10秒的吐司服务。

MainActivity.java

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);//load the layout file 
     startService(new Intent(this,MyService.class));//use to start the services 
    } 
} 

MyService.java

public class MyService extends Service { 
    public static final long INTERVAL=10000;//variable to execute services every 10 second 
    private Handler mHandler=new Handler(); // run on another Thread to avoid crash 
    private Timer mTimer=null; // timer handling 
    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     throw new UnsupportedOperationException("unsupported Operation"); 
    } 
    @Override 
    public void onCreate() { 
     // cancel if service is already existed 
      if(mTimer!=null) 
       mTimer.cancel(); 
     else 
       mTimer=new Timer(); // recreate new timer 
     mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(),0,INTERVAL);// schedule task 
    } 
    @Override 
    public void onDestroy() { 
     Toast.makeText(this, "In Destroy", Toast.LENGTH_SHORT).show();//display toast when method called 
     mTimer.cancel();//cancel the timer 
    } 
    //inner class of TimeDisplayTimerTask 
    private class TimeDisplayTimerTask extends TimerTask { 
     @Override 
     public void run() { 
      // run on another thread 
      mHandler.post(new Runnable() { 
       @Override 
       public void run() { 
        // display toast at every 10 second 
        Toast.makeText(getApplicationContext(), "Notify", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
    } 
} 

AndroidManifest.xml中

<service android:name=".MyService" 
     android:enabled="true"/>