80,488
社区成员
发帖
与我相关
我的任务
分享
package lab.sodino.serviceactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Act extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnStartService = (Button) findViewById(R.id.btnStartService);
btnStartService.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(Act.this, MainService.class);
Act.this.startService(intent);
}
});
}
}
package lab.sodino.serviceactivity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MainService extends Service {
public IBinder onBind(Intent arg0) {
return null;
}
public void onStart(Intent intent, int startID) {
Log.d("ANDROID_INFO", "Service onStart:startID=" + startID);
Intent tancIntent = new Intent(getApplicationContext(),
TestActivity.class);
tancIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(tancIntent);
Log.d("ANDROID_INFO", "Service onStart End");
}
}
package lab.sodino.serviceactivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class TestActivity extends Activity {
public void onCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//以下两句Log都没有被输出,onCreate()方法未被运行。屏幕全部是黑色
Log.d("ANDROID_INFO","TANCActivity.onCreated Start");
TextView textView = new TextView(this);
textView.setText("Show TestActivity");
textView.setTextSize(20.0f);
textView.setTextColor(0xff0000ff);
textView.setBackgroundColor(0xffffffff);
setContentView(textView);
Log.d("ANDROID_INFO","TANCActivity.onCreated OK");
}
}
LinearLayout layoutParent = new LinearLayout(getContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setText("Show TestActivity");
textView.setTextSize(20.0f);
textView.setTextColor(0xff0000ff);
textView.setBackgroundColor(0xffffffff);
layoutParent.addView(textView);
setContentView(textView);