2013年8月2日星期五

About BroadcastReceiver start the Service issue.

I need to use a boot of BroadcastReceiver starts a Service, and then service, using WindowManager own definition of a button will be added to a running view above.
but I do not know where the wrong, please help busy, look.

MyReceiver.java boot-up, the receiver, and the receiver to start the service defined below

package com.sav;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, SAV.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(i);
Log.v("receiver" , "on receive");
}
}



MyServer.java defines its own service, will own custom button added to the front view is now running.

package com.sav;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.view.WindowManager;
import android.widget.Button;

public class MyServer extends Service {

public final static String FIELD_EVENT = "event";
private WindowManager mWm;
private Button buttonview;
WindowManager.LayoutParams mParams;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
mWm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
buttonview = new Button(this);
buttonview.setText("window manager test!");
mParams = new WindowManager.LayoutParams();
mParams.height = 50;
mParams.width = 50;
}

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
mWm.addView(buttonview, mParams);
}
}


SAV.java a foreground program runs.

package com.sav;

import android.app.Activity;
import android.os.Bundle;

public class SAV extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


Here is AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sav" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SAV" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyServer"></service>
</application>
</manifest>


The above is all the procedures, do not know why BroadcastReceiver's onReceive method is not starting?
Thank you. Help look.


------ Solution ------------------------------------ --------
try again to add permissions

------ Solution ------------- -------------------------------
do not know why BroadcastReceiver The onReceive method is not starting?
What do you mean, onReceive method has not been called to it? Your first line of the onReceive method plus a Log, if the sentence did not perform to, below even think about it. There is obviously a startService
Why is an activity to set the class??

------ Solution ------------------------------------ --------

plus look, but do not add it to the unwary label inside. .
------ Solution ---------------------------------------- ----
public class SAV extends Activity / / SAV.class is the Activity class

Intent i = new Intent (context, SAV.class);
i.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService (i) ;/ / boot should use startActivity (i)
Log.v ("receiver", "on receive");


------ Solution ------------------------------------ --------
receiver how you can start, the landlord said about ah?

------ For reference only ---------------------------------- -----
still no response.
is this someone replies
I think your program is very problematic:
1. in your MyReceiver
Intent i = new Intent (context, SAV.class);
i.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService (i);
Log.v ("receiver", "on receive");
Here you should open a
SAV.class ia an activity, not service, you should call context.startActivity (intent)
If you are determined to be open context.startService (MyServer);
But your service before the activity running, you can not find the activity. Additionally, service will not be processed about the view related content, so you define the service inside buttonview is wrong.


But I how to become a service activity of the?
service is not related to the contents of the view-what? However, in http:/ / stackoverflow.com/questions/3734099/how-can-i-put-my-popupwindow-to-system-desk-or-other-app , inside the service added view. It is what principle?
------ For reference only -------------------------------------- -

set wrong ah.
Why onReceive method did not start it?





Why is this a problem?
------ For reference only -------------------------------------- -

right, this is wrong. I changed it.
------ For reference only -------------------------------------- -
receiver can start now, but exceptions are:
unable to start service, WindowManager $ BadTokenException: unable to add window, token null is not for an application.

public class MyServer extends Service {

    public final static String FIELD_EVENT = "event";
    private WindowManager mWm;
    private Button buttonview;
    WindowManager.LayoutParams mParams;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        mWm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        buttonview = new Button(this);
        buttonview.setText("window manager test!");
        mParams = new WindowManager.LayoutParams();
        mParams.height = 50;
        mParams.width = 50;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        mWm.addView(buttonview, mParams);
    }
}

is not the inside when there are problems in addview?
------ For reference only -------------------------------------- -





then:

receiver is a start-up.
But the service is still abnormal, please help.
------ For reference only -------------------------------------- -
reference http://aijiawang-126-com.iteye.com/blog/662336
Only one Activity to add a form
------ For reference only -------------------------- -------------

not home add view?
If at home because you can not add a view, you want to add a custom home on something, you can accept the click event, then use toast can it? Or are there other stuff.
------ For reference only -------------------------------------- -
A toast can not accept user interaction events.
developer.android.com inside is not, and said the same, toast does not accept what-click operation?
I want a front things start other activity. The front desk is always something or other activity at home. But if you want to start another activity, there must be the trigger is not? How does not accept interaction events trigger?

I looked dialog things, I feel that it is difficult to achieve the effect I want.
There are other that can be embedded in the home and other activity on top of things it?
------ For reference only -------------------------------------- -
their top.
------ For reference only -------------------------------------- -
import



没有评论:

发表评论