从Service中启动Acitivity时向Activity中传递参数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/jing_huanhuan/article/details/6122870

在service中写的方法启动activity,让activity获取传递的值:

      service端的代码:

            Intent video_test_intent = new Intent("com.broncho.intent.action.VIDEO_TEST");           
            video_test_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            video_test_intent.putExtra("run_time", 5);
            video_test_intent.putExtra("break_time", 10);
            startActivity(video_test_intent);

 

     被启动的Activity端获取传递过来的参数代码:

        public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);

 

         .......


        Log.w("TAG", "run_time = " + this.getIntent().getExtras().get("run_time"));    //=5
        Log.w("TAG", "break time = " + this.getIntent().getExtras().get("break_time"));   //=10    
    }

 

 2011-01-07

 

展开阅读全文

Service启动Activity失败

11-11

程序运行后启动类是Act,点击Act中的StartService按钮启动MainService,MainService.onStart()方法中调用 startActivity(intent)来启动TestActivity,最后结果是TestActivity并没有显示出来,屏幕全部是黑色的。程序没有报错。rnActivity与Service都已在AndroidManifest.xml中注册了rnrn请问这是怎么回事呢?谢谢...rn代码如下:rnrn[code=Java]rnpackage lab.sodino.serviceactivity;rnrnimport android.app.Activity;rnimport android.content.Intent;rnimport android.os.Bundle;rnimport android.view.View;rnimport android.widget.Button;rnrnpublic class Act extends Activity rn /** Called when the activity is first created. */rn @Overridern public void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.main);rn Button btnStartService = (Button) findViewById(R.id.btnStartService);rn btnStartService.setOnClickListener(new Button.OnClickListener() rn public void onClick(View view) rn Intent intent = new Intent(Act.this, MainService.class);rn Act.this.startService(intent);rn rn );rn rnrn[/code]rnrn[code=Java]rnpackage lab.sodino.serviceactivity;rnrnimport android.app.Service;rnimport android.content.Intent;rnimport android.os.IBinder;rnimport android.util.Log;rnrnpublic class MainService extends Service rnrn public IBinder onBind(Intent arg0) rn return null;rn rnrn public void onStart(Intent intent, int startID) rn Log.d("ANDROID_INFO", "Service onStart:startID=" + startID);rn Intent tancIntent = new Intent(getApplicationContext(),rn TestActivity.class);rn tancIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);rn startActivity(tancIntent);rn Log.d("ANDROID_INFO", "Service onStart End");rn rnrnrn[/code]rnrn[code=Java]rnpackage lab.sodino.serviceactivity;rnrnimport android.app.Activity;rnimport android.os.Bundle;rnimport android.util.Log;rnimport android.widget.TextView;rnrnpublic class TestActivity extends Activity rn public void onCreated(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn //以下两句Log都没有被输出,onCreate()方法未被运行。屏幕全部是黑色rn Log.d("ANDROID_INFO","TANCActivity.onCreated Start");rn TextView textView = new TextView(this);rn textView.setText("Show TestActivity");rn textView.setTextSize(20.0f);rn textView.setTextColor(0xff0000ff);rn textView.setBackgroundColor(0xffffffff);rn setContentView(textView);rn Log.d("ANDROID_INFO","TANCActivity.onCreated OK");rn rnrnrn[/code] 论坛

没有更多推荐了,返回首页