android服务与activity通讯

来源:互联网 发布:找淘宝达人推广的技巧 编辑:程序博客网 时间:2024/04/30 06:47
package com.example.gametest;


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


public class Myservice extends Service
{


@Override
public IBinder onBind(Intent intent) 
{

return null;
}


@Override
public void onCreate() 
{

super.onCreate();
new testthread().start();
}


@Override
public void onStart(Intent intent, int startId)
{

super.onStart(intent, startId);

String info=intent.getStringExtra("test");
System.out.println("copy activity send info is:"+info);
}

private class testthread extends Thread
{
public void run()
{
while(true)
{
try 
{
Thread.sleep(3000);

Intent ii=new Intent("test");
ii.putExtra("ss","service send info to activity");
sendBroadcast(ii);
} catch (InterruptedException e)
{

e.printStackTrace();
}
}


}
}


}

//下面是Activity

package com.example.gametest;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity 
{

private Button b1;
private Button b2;


@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);

b1.setOnClickListener(new OnClickListener()
{


@Override
public void onClick(View v)
{
Intent i=new Intent(MainActivity.this,Myservice.class);
i.putExtra("test", "activity send info to service");
startService(i);

}

}); 


}

@Override
protected void onStart() 
{

super.onStart();

brodcast bb=new brodcast();
IntentFilter filter=new IntentFilter("test");
this.registerReceiver(bb, filter);
}



private class brodcast extends BroadcastReceiver
{


@Override
public void onReceive(Context context, Intent intent) 
{
String info=intent.getStringExtra("ss");
System.out.println(info);


}

}
}

//布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="56dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="启动服务" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="61dp"
        android:text="停止服务" />


</RelativeLayout>

0 0
原创粉丝点击