开发者

Repaint Activity through Service

开发者 https://www.devze.com 2023-03-28 21:59 出处:网络
I use my Main Activity to paint Text. Now Ive a Service in Background that listens to a bluetooth device. If a button on the device is pressed I insert values in the local database and want to repaint

I use my Main Activity to paint Text. Now Ive a Service in Background that listens to a bluetooth device. If a button on the device is pressed I insert values in the local database and want to repaint the correct value in my Main Activity. Everything works fine - just the repaint does not work, because i cant call the invalidate() method from my Service.

I tried with a BroadCastReceiver but i think i misunderstood something.


This is my Method in my Service "ControllerService"

private void insertToDB(int iWert, char c) {
   if(zt.getJoystickMoved().x == iWert) {
      sendOrderedBroadcast(i,null);
      Log.i("Broadcast","send!");
      //in this block i want to noti开发者_如何学JAVAfy the Activity
   }
}

Here is my Main Activity

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   registerReceiver(resetReceiver, new IntentFilter("my.intent.INTENT_NAME"));
}

public BroadcastReceiver resetReceiver = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {
      Log.i("Broadcast", "got Broadcast!");
      MainActivity.this.cc.invalidate();
      //cc.invalidate() repaints my Activity
      //cc is a View with the paint Logic inside
      this.setResultCode(Activity.RESULT_OK);
   }
}

My Broadcast Receiver in my Main Activity does not receive anything ... I hope someone could help me with my Problem.


In my AndroidManifest is this code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test.countV1"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission
        android:name="android.permission.BLUETOOTH" />
    <uses-permission 
        android:name="android.permission.BLUETOOTH_ADMIN" />

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:name="com.test.data.ListHelper">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="my.intent.INTENT_NAME"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:label="PreferencesActivity" android:name="PreferencesActivity"></activity>
        <activity android:name="ZtPreferencesActivity" android:label="ZtPreferencesActivity"></activity>
        <activity android:name="customizePreferences"></activity>
        <activity android:name="ControllerActivity"></activity>
        <service android:name="com.test.services.ControllerService"></service>
        <activity android:name="BatteryActivity"></activity>
    </application>
</manifest>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号