/**
* Created by zxn on 7/16/2016.
* 说明:开启一个Service服务,在服务中开启一个线程并定义一个全局变量i和j,每2秒钟i+1,j*2并分别更新Activity中的两个TextView
*/
public class Activity_BroadCast_Work extends Activity
{
TextView textViewi;
TextView textViewj;
IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcast_work);
intentFilter = new IntentFilter();
intentFilter.addAction("signel");
textViewi = (TextView)findViewById(R.id.texti);
textViewj = (TextView)findViewById(R.id.textj);
Intent intent = new Intent();
intent.setClass(Activity_BroadCast_Work.this, Service_BroadCast_Work.class);
startService(intent);
}
@Override
protected void onStart()
{
registerReceiver(printbroadcast,intentFilter);
super.onStart();
}
@Override
protected void onDestroy()
{
super.onDestroy();
unregisterReceiver(printbroadcast);
}
BroadcastReceiver printbroadcast = new BroadcastReceiver()
{
static final String name = "signel";
@Override
public void onReceive(Context context, Intent intent)
{
if (name.equals(intent.getAction()))
{
textViewi.setText(intent.getStringExtra("i"));
textViewj.setText(intent.getStringExtra("j"));
}
}
};
}
/**
* Created by zxn on 7/16/2016.
* 说明:
*/
public class Service_BroadCast_Work extends Service
{
@Nullable
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId)
{
final Runnable newThread = new Runnable()
{
int i = 0;
int j = 1;
@Override
public void run()
{
while(true)
{
i++;
j *= 2;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent broadcaseintent = new Intent();
broadcaseintent.putExtra("i",i+"");
broadcaseintent.putExtra("j",j+"");
broadcaseintent.setAction("name_a");
sendBroadcast(broadcaseintent);
}
}
};
Thread newThreadd = new Thread(newThread);
newThreadd.start();
return super.onStartCommand(intent, flags, startId);
}
}
//activity_broadcast_work.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service—Update—I:"/>
<TextView
android:id="@+id/texti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新i"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service—Update—J:"/>
<TextView
android:id="@+id/textj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新i"/>
</LinearLayout>
</LinearLayout>