Android 学习历程之二 如何在一个Service中调用Activity(Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK))

在一个Service中调用一个Activity,因为没有上下文对象,会报 android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag 错误。解决办法,就是在创建一个新的Task任务(Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)).

实例代码如下:

package com.yingliandroid_sdk_demo.EX_6;

import com.yingliandroid_sdk_demo.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MySetviceActivity extends Activity {

private Button startOn;
private Button endOff;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.service01);

startOn = (Button) findViewById(R.id.startOn);
endOff = (Button) findViewById(R.id.endOff);

startOn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent intent = new Intent(MySetviceActivity.this, MyService01.class);

startService(intent);

}
});

endOff.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent intent = new Intent(MySetviceActivity.this, MyService01.class);
stopService(intent);
}
});
}
}


package com.yingliandroid_sdk_demo.EX_6;


import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.provider.MediaStore.Video;
import android.util.Log;


public class MyService01 extends Service {


private Handler handler = new Handler();

private int intCountter = 0;

// private Runnable myTask = new Runnable() {
//
// @Override
// public void run() {
//
// intCountter++;
//
// Log.i("Hippo", "counter :"+Integer.toString(intCountter));
//
// handler.postDelayed(myTask, 1000);
// }
// };

public void onStart(Intent intent, int startId) {

// handler.postDelayed(myTask, 1000);

     Log.i("ANDROID_INFO", "Service onStart:startID=" + startId);
    Intent tancIntent = new Intent(getApplicationContext(),
    TestActivity.class);

/**
     * 关键代码,必须重启一个新任务
     */
    

    tancIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(tancIntent);
    Log.i("ANDROID_INFO", "Service onStart End");

};


@Override
public void onDestroy() {

super.onDestroy();
}


@Override
public IBinder onBind(Intent intent) {

return null;
}
}


package com.yingliandroid_sdk_demo.EX_6;


import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class TestActivity extends Activity {


private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


LinearLayout layoutParent = new LinearLayout(this);
        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);
        
        setContentView(layoutParent);
}

}


你可能感兴趣的:(android学习历程笔记)