2012-04-06 4 views
1

Ich fange an, meine Haare über diese ziehen! Ich habe versucht, eine App zu machen, die ein Widget auf dem Bildschirm platziert, die, wenn sie angeklickt wird, das Telefon neu starten. Das Widget wird auf dem Bildschirm angezeigt, aber beim Klicken passiert nichts. Ich habe Buckys Tutorial auf youtube verfolgt.Android App Widget, das Telefon neu starten wird - Widget tut nichts?

Der Code ist hier, ich weiß nicht, was ich falsch mache!

reboot_widget_activeity.java:

package com.liamwli.reboot_widget; 

import android.app.Activity; 
import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RemoteViews; 

public class reboot_widget_activeity extends Activity implements 
     OnClickListener { 
    /** Called when the activity is first created. */ 
    int awID; 
    AppWidgetManager awm; 
    Context c = reboot_widget_activeity.this; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     Intent i = getIntent(); 
     Bundle extras = i.getExtras(); 
     if (extras != null) { 
      awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, 
        AppWidgetManager.INVALID_APPWIDGET_ID); 
     } 
     awm = AppWidgetManager.getInstance(c); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout); 

     Intent in = new Intent(c, Reboot.class); 
     PendingIntent pi = PendingIntent.getActivity(c, 0, in, 0); 
     views.setOnClickPendingIntent(R.id.bRB, pi); 

     awm.updateAppWidget(awID, views); 

     Intent result = new Intent(); 
     result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID); 
     setResult(RESULT_OK, result); 
     finish(); 
    } 
} 

Reboot.java:

package com.liamwli.reboot_widget; 

import java.io.IOException; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.widget.Toast; 

public class Reboot extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     ProgressDialog wait; 
     wait = new ProgressDialog(Reboot.this); 
     wait.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     wait.setMessage("Rebooting. If this hangs, then this app wont work on this device (pull battery)"); 
     wait.setCancelable(false); 
     wait.show(); 
     try { 
      Runtime.getRuntime().exec("su"); 
      Runtime.getRuntime().exec("reboot"); 
     } catch (IOException e) { 
     } 
     wait.dismiss(); 
     Toast.makeText(Reboot.this, "Unable to reboot. Please ensure your device is rooted!", Toast.LENGTH_LONG).show(); 
     finish(); 
    } 

} 

widget.java:

package com.liamwli.reboot_widget; 

import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.widget.Toast; 

public class Widget extends AppWidgetProvider { 

    @Override 
    public void onDeleted(Context context, int[] appWidgetIds) { 
     // TODO Auto-generated method stub 
     super.onDeleted(context, appWidgetIds); 
     Toast.makeText(context, "Reboot Widget Added", Toast.LENGTH_SHORT) 
       .show(); 
    } 


} 

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.liamwli.reboot_widget" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".reboot_widget_activeity" 
      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=".Widget"> 

     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     </intent-filter> 

     <meta-data android:name="android.appwidget.provider" android:resource="@xml/widgetstuff"/> 
    </receiver> 

    <activity 
      android:name=".Reboot" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.liamwli.reboot_widget.REBOOT" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 




    </application> 

</manifest> 

widgetstuff.xml:

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minHeight="40dp" android:minWidth="40dp" android:initialLayout="@layout/widget_layout"> 


</appwidget-provider> 

widget_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/bRB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Reboot" /> 

</LinearLayout> 

Eklipse gibt mir keine Fehler, und ich nicht irgendwelche Kraft schließt bekommen. Die Schaltfläche auf dem Widget tut einfach nichts - warum?

Antwort

2

Änderung dieser:

Runtime.getRuntime().exec("su"); 
Runtime.getRuntime().exec("reboot"); 

dazu:

Runtime.getRuntime().exec("su -c reboot"); 

Grund: Sie kippe trennen die beiden Befehle, weil dann su separat läuft und Neustart läuft ohne su

P.S. Vergessen Sie nicht, die erforderliche Berechtigung hinzuzufügen

+0

Dies hat nicht funktioniert. Es ist so, als ob die Reboot-Klasse nicht aufgerufen wird, wenn die Widget-Schaltfläche gedrückt wird. –

+0

Warum rufst du einfach 'startActivity (neue Absicht (reboot_widget_activity.this, Reboot.class));' in ** onClick ** event? – waqaslam

+0

Es ist ein Widget - kann ich das nicht tun kann ich? –

1

Die Berechtigung fehlt im Manifest. Beachten Sie, dass il nur auf einem tatsächlichen Gerät funktioniert, nicht in dem Emulator

<uses-permission android:name="android.permission.REBOOT" /> 
+0

Würde dies jedoch benötigt werden, da es den Befehl root verwendet? –

+0

Ok, ich habe das getan und es hat nicht funktioniert - es ist so, als würde die Reboot-Klasse nicht aufgerufen. –