WiFi deaktivieren

  • Antworten:2
Addyk2
  • Forum-Beiträge: 21

28.08.2013, 22:07:46 via Website

Hallo,

meine App soll wenn man auf den Button drückt, wifi deaktivieren, aber sie hängt sich jedesmal wenn man draufdrückt auf, und wird dann angehalten.
Permissions habe ich aber alle in der manifest.xml aufgeführt :-(

Kann mir jemand helfen?

Hier die manifest.xml, MainActivity.java und activity_main.xml:

1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.testapp"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
8 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
9
10 <uses-sdk
11 android:minSdkVersion="7"
12 android:targetSdkVersion="16" />
13
14 <application
15 android:allowBackup="true"
16 android:icon="@drawable/ic_launcher"
17 android:label="@string/app_name"
18 android:theme="@style/AppTheme" >
19 <activity
20 android:name="com.example.testapp.MainActivity"
21 android:label="@string/app_name" >
22 <intent-filter>
23 <action android:name="android.intent.action.MAIN" />
24
25 <category android:name="android.intent.category.LAUNCHER" />
26 </intent-filter>
27 </activity>
28 </application>
29
30</manifest>

1package com.example.testapp;
2
3import android.app.Activity;
4import android.content.Context;
5import android.os.Bundle;
6import android.view.Menu;
7import android.view.View;
8import android.widget.Button;
9import android.net.wifi.WifiManager;
10import android.widget.TextView;
11
12public class MainActivity extends Activity {
13
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.activity_main);
18
19 }
20
21
22 @Override
23 public boolean onCreateOptionsMenu(Menu menu) {
24 final Button btn1=new Button (this);
25 final TextView tv1=new TextView(this);
26 // Inflate the menu; this adds items to the action bar if it is present.
27 getMenuInflater().inflate(R.menu.main, menu);
28 View.OnClickListener buttonListener = new View.OnClickListener() {
29
30 @Override
31 public void onClick(View v) {
32
33 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
34 wifi.setWifiEnabled(false);
35 tv1.setText("Test");
36 }
37 };
38 btn1.setOnClickListener(buttonListener);
39 return true;
40 }
41
42}

1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:paddingLeft="@dimen/activity_horizontal_margin"
6 android:paddingRight="@dimen/activity_horizontal_margin"
7 android:paddingTop="@dimen/activity_vertical_margin"
8 android:paddingBottom="@dimen/activity_vertical_margin"
9 tools:context=".MainActivity">
10
11 <TextView
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:textAppearance="?android:attr/textAppearanceSmall"
15 android:text="Small Text"
16 android:id="@+id/tv1"
17 android:layout_centerVertical="true"
18 android:layout_centerHorizontal="true" />
19
20 <Button
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:text="New Button"
24 android:id="@+id/btn1"
25 android:layout_alignParentBottom="true"
26 android:layout_centerHorizontal="true"
27 android:onClick="method"
28 android:layout_marginBottom="48dp" />
29
30</RelativeLayout>

Antworten
Michele
  • Forum-Beiträge: 1.525

28.08.2013, 22:36:52 via Website

Hallo.

Du sagst durch ein Button klick?

Wo ist der Button bitte in der onCreate?
Woher soll deine Activity wissen welcher Button?
Oder welcher Text?

Darf ich fragen woher du den mist kopiert hast?

Und immer LogCat zeigen bitte für Fehler Meldung.


LG

— geändert am 28.08.2013, 22:41:04

Antworten
Addyk2
  • Forum-Beiträge: 21

29.08.2013, 11:27:01 via Website

Den deiner Meinung nach "Mist" habe ich aus einem Beispielprojekt kopiert. :grin:

Aber du hattest Recht, hatte den Button nicht in OnCreate, sondern in dem OnCreate Optionsmenu fälschlicherweise...

Jetzt gehts...

Antworten