服务代码
package com.example.app2.Evening;
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.net.Uri;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
-
An {@link IntentService} subclass for handling asynchronous task requests in
-
a service on a separate handler thread.
-
-
TODO: Customize class - update intent actions, extra parameters and static
-
helper methods.
*/
public class MyIntentService extends IntentService {public MyIntentService( ){
super(“MyIntentService”);
}public MyIntentService( String name){
super( name);
}@Override
protected void onHandleIntent(Intent intent) {
StringBuffer sb = new StringBuffer();
try {
URL url=new URL( “http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1”);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.connect();
if( uc.getResponseCode()==200){
InputStream is = uc.getInputStream();
byte[] arr=new byte[1024];
int len=0;
while( (len=is.read(arr))!=-1){} } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
}
}
主界面代码
package com.example.app2.Evening;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.app2.R;
public class EP extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ep);
}
}
Java代码
package com.example.app2;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = “123321”;
public MyService() {
// Log.i(TAG, “MyService: 创建了服务。”);
// Notification.Builder builder = new Notification.Builder(getApplicationContext());
// Intent it = new Intent(this, MainActivity.class);
// builder.setContentIntent(PendingIntent.
// getActivity(this,0,it,0))//设置PendingIntent
// .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher_round))//设置下拉列表中的图标
// .setContentTitle(“下拉列表的Title”)//设置下拉列表的title
// .setSmallIcon(R.mipmap.ic_launcher)//设置状态栏内的图片
// .setContentText(“要显示的内容。”)//设置上下文内容。
// .setWhen(System.currentTimeMillis());//设置通知发生的时间。
//
// Notification build = builder.build();//获取构建好的Notification
// build.defaults=Notification.DEFAULT_SOUND;//设置默认的声音。
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: 创建了服务。");
Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
Intent it = new Intent(this, MainActivity.class);
builder.setContentIntent(PendingIntent.
getActivity(this,0,it,0))//设置PendingIntent
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher_round))//设置下拉列表中的图标
.setContentTitle("下拉列表的Title")//设置下拉列表的title
.setSmallIcon(R.mipmap.ic_launcher)//设置状态栏内的图片
.setContentText("要显示的内容。")//设置上下文内容。
.setWhen(System.currentTimeMillis());//设置通知发生的时间。
Notification build = builder.build();//获取构建好的Notification
build.defaults=Notification.DEFAULT_SOUND;//设置默认的声音。
startForeground(10000,build);//通知。
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: 绑定。");
return null;
}
}
intent代码
package com.example.app2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button mainButton1;
private Button mainButton2;
private Intent it;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainButton1 = (Button) findViewById(R.id.main_button1);
mainButton2 = (Button) findViewById(R.id.main_button2);
}
public void click(View view) {
switch (view.getId()) {
case R.id.main_button1:
it=new Intent(MainActivity.this,MyService.class);
startService(it);
break;
case R.id.main_button2:
stopService(it);
break;
}
}
}