服务Service

创建类继承Service
重写方法
在清单文件注册

package com.example.myapplication;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**

  • 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 {
    // TODO: Rename actions, choose action names that describe tasks that this
    // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
    private static final String ACTION_FOO = “com.example.myapplication.action.FOO”;
    private static final String ACTION_BAZ = “com.example.myapplication.action.BAZ”;

    // TODO: Rename parameters
    private static final String EXTRA_PARAM1 = “com.example.myapplication.extra.PARAM1”;
    private static final String EXTRA_PARAM2 = “com.example.myapplication.extra.PARAM2”;

    public MyIntentService() {
    super(“MyIntentService”);
    }

    /**

    • Starts this service to perform action Foo with the given parameters. If
    • the service is already performing a task this action will be queued.
    • @see IntentService
      */
      // TODO: Customize helper method
      public static void startActionFoo(Context context, String param1, String param2) {
      Intent intent = new Intent(context, MyIntentService.class);
      intent.setAction(ACTION_FOO);
      intent.putExtra(EXTRA_PARAM1, param1);
      intent.putExtra(EXTRA_PARAM2, param2);
      context.startService(intent);
      }

    /**

    • Starts this service to perform action Baz with the given parameters. If
    • the service is already performing a task this action will be queued.
    • @see IntentService
      */
      // TODO: Customize helper method
      public static void startActionBaz(Context context, String param1, String param2) {
      Intent intent = new Intent(context, MyIntentService.class);
      intent.setAction(ACTION_BAZ);
      intent.putExtra(EXTRA_PARAM1, param1);
      intent.putExtra(EXTRA_PARAM2, param2);
      context.startService(intent);
      }

    @Override
    protected void onHandleIntent(Intent intent) {
    co co = new co();
    String json = co.getJson();
    Intent intent1 = new Intent();
    intent1.setAction(“com.spark”);
    Bundle bundle = new Bundle();
    bundle.putString(“json”,json.toString());
    intent1.putExtras(bundle);
    sendBroadcast(intent1);
    }

    /**

    • Handle action Foo in the provided background thread with the provided
    • parameters.
      */
      private void handleActionFoo(String param1, String param2) {
      // TODO: Handle action Foo
      throw new UnsupportedOperationException(“Not yet implemented”);
      }

    /**

    • Handle action Baz in the provided background thread with the provided
    • parameters.
      */
      private void handleActionBaz(String param1, String param2) {
      // TODO: Handle action Baz
      throw new UnsupportedOperationException(“Not yet implemented”);
      }
      }

package com.example.myapplication;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class Main2Activity extends AppCompatActivity {

private MyService myService;
private Intent intent;
private MyReceiver myReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);


}

private void registReceiver() {
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.spark");
    myReceiver = new MyReceiver();
    registerReceiver(myReceiver,intentFilter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    stopService(intent);
    unregisterReceiver(myReceiver);
}

public void click(View view) {
    registReceiver();

    intent=new Intent(this,MyIntentService.class);
    startService(intent);
}

}
package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
private static final String TAG = “MyReceiver”;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(“com.spark”.equals(action)){
Bundle extras = intent.getExtras();
String json = extras.getString(“json”, “”);
Log.i(TAG, "onReceive: "+json);
Toast.makeText(context, json, Toast.LENGTH_SHORT).show();
}
}
}

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class co {

public static String getJson(){
    StringBuffer sb = new StringBuffer();
    HttpURLConnection connection=null;
    InputStream is=null;

    try {

        URL url = new URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
        connection=(HttpURLConnection)url.openConnection();
        connection.setReadTimeout(5*1000);
        connection.setConnectTimeout(5*1000);

        if(connection.getResponseCode()==200){
            is=connection.getInputStream();
            byte[] b=new byte[1024];
            int len=0;
            while((len=is.read(b))!=-1){
                sb.append(new String(b,0,len));
            }


            return sb.toString();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        if(is!=null){
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null){
            connection.disconnect();
        }
    }
    return sb.toString();
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值