7.22当堂代码

服务代码

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值